当前位置:   article > 正文

Xamarin.Android | 界面跳转到手机自带的自启动管理界面,引导用户将APP加入自启动_安卓跳转到自启动管理意图

安卓跳转到自启动管理意图

在这里插入图片描述

Xamarin.Android | 界面跳转到手机自带的自启动管理界面,引导用户将APP加入自启动

前言

很多 Android 应用需要在后台运行,以便提供实时通知、定时任务等服务,但是部分 Android 系统通过限制应用程序后台运行,以降低电池消耗和提高系统性能。这就可能导致应用程序运行不稳定,通知延迟,定时任务无法正常执行等问题。因此,在某些情况下,将应用程序添加到自启动白名单中可能会提高应用程序的运行稳定性和响应速度。

为了帮助用户在使用 APP 时提高其稳定性和使用体验,有时候我们需要让安卓手机的界面跳转到手机自带的自启动管理界面,以此来引导用户将 APP 加入自启动,确保应用程序在后台运行时不被系统杀死,从而保证应用程序服务的稳定性和可靠性。同时,这也可以提高用户的使用体验,使用户能够更好地享受应用程序的功能和服务。

示例代码

这部分代码是使用C#语言在Xamarin.Android框架下实现的,我猜大多是看这篇文章的都是Java开发者,其实uri都一样,换成HashMap就好了。

自启动界面URI字典

        private static Dictionary<string, string[]> GetManagerpageUriDictionary()
        {
            Dictionary<string, string[]> dict = new Dictionary<string, string[]>();

            // 小米 & 红米 & 黑鲨 & POCO
            dict["xiaomi"] = dict["redmi"] = dict["blackshark"] = new string[] {
                    "com.miui.securitycenter/com.miui.permcenter.autostart.AutoStartManagementActivity",
                    "com.miui.securitycenter",

                    "com.miui.securitycenter/com.miui.powercenter.PowerSettings",
                    "com.miui.securitycenter/com.miui.powercenter.PowerSettingsActivity",
                    "com.miui.powerkeeper/.ui.HiddenAppsContainerManagementActivity",
                    "com.miui.powerkeeper/.ui.HiddenAppsConfigActivity",
                    "com.miui.powerkeeper/.ui.HiddenAppsConfigActivityAlias",
                    "com.miui.powerkeeper/.ui.HiddenAppsContainerManagementActivityAlias",
                    "com.miui.powerkeeper/.ui.HiddenAppsConfigActivity2",
                    "com.miui.powerkeeper/.ui.HiddenAppsConfigActivityAlias2",
                    "com.miui.powerkeeper/.ui.HiddenAppsContainerManagementActivityAlias2",
                    "com.miui.powerkeeper/.ui.HiddenAppsConfigActivity3",
                    "com.miui.powerkeeper/.ui.HiddenAppsConfigActivityAlias3",
                    "com.miui.powerkeeper/.ui.HiddenAppsContainerManagementActivityAlias3",
            };
            // 三星
            dict["samsung"] = new string[] {
                    "com.samsung.android.sm_cn/com.samsung.android.sm.ui.ram.AutoRunActivity",
                    "com.samsung.android.sm_cn/com.samsung.android.sm.ui.appmanagement.AppManagementActivity",
                    "com.samsung.android.sm_cn/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity",
                    "com.samsung.android.sm_cn/.ui.ram.RamActivity",
                    "com.samsung.android.sm_cn/.app.dashboard.SmartManagerDashBoardActivity",

                    "com.samsung.android.sm/com.samsung.android.sm.ui.ram.AutoRunActivity",
                    "com.samsung.android.sm/com.samsung.android.sm.ui.appmanagement.AppManagementActivity",
                    "com.samsung.android.sm/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity",
                    "com.samsung.android.sm/.ui.ram.RamActivity",
                    "com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity",

                    "com.samsung.android.lool/com.samsung.android.sm.ui.battery.BatteryActivity",
                    "com.samsung.android.sm_cn",
                    "com.samsung.android.sm"
            };
            // 华为 & 荣耀
            dict["huawei"] = dict["honor"] = new string[] {
                    "com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity",
                    "com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity",
                    "com.huawei.systemmanager/.optimize.process.ProtectActivity",
                    "com.huawei.systemmanager/.optimize.bootstart.BootStartActivity",
                    "com.huawei.systemmanager"
            };
            // VIVO
            dict["vivo"] = new string[] {
                    "com.iqoo.secure/.ui.phoneoptimize.BgStartUpManager",
                    "com.iqoo.secure/.safeguard.PurviewTabActivity",
                    "com.vivo.permissionmanager/.activity.BgStartUpManagerActivity",
                    "com.iqoo.secure",
                    "com.vivo.permissionmanager",

                    "com.vivo.permissionmanager/.activity.PurviewTabActivity",
                    "com.vivo.abeui/.manager.VivoAutoLaunchManagerActivity",
                    "com.iqoo.secure/.ui.phoneoptimize.AddWhiteListActivity",
                    "com.iqoo.secure/.safeguard.SoftPermissionDetailActivity",
            };
            // 魅族
            dict["meizu"] = new string[] {
                    "com.meizu.safe/.permission.SmartBGActivity",
                    "com.meizu.safe/.permission.PermissionMainActivity",
                    "com.meizu.safe"
            };
            // OPPO
            dict["oppo"] = new string[] {
                    "com.coloros.safecenter/.startupapp.StartupAppListActivity",
                    "com.coloros.safecenter/.permission.startup.StartupAppListActivity",
                    "com.oppo.safe/.permission.startup.StartupAppListActivity",
                    "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity",
                    "com.color.safecenter/.permission.PermissionTopActivity",
                    "com.color.safecenter/.permission.startup.StartupAppListActivity",
                    "com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity",
                    "com.coloros.safecenter",
                    "com.color.safecenter",
                    "com.oppo.safe",
                    "com.coloros.oppoguardelf",
                    "com.coloros.safecenter/.startupapp.AssociateStartActivity",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForExternal",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForLauncher",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForIcon",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKey",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForExternal",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForLauncher",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForIcon",
                    "com.coloros.safecenter/.PermissionTopActivity",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivityForExternal",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivityForLauncher",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivityForIcon",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForExternal",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForLauncher",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForIcon",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKey",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForExternal",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForLauncher",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForIcon",
                    "com.coloros.safecenter/.permission.topactivity.PermissionTopActivity",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivity",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivityForExternal",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivityForLauncher",
                    "com.coloros.safecenter/.permission.startupapp.StartupAppListActivityForIcon",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForExternal",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForLauncher",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForIcon",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKey",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForExternal",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForLauncher",
                    "com.coloros.safecenter/.startupapp.StartupAppListActivityForOneKeyForIcon",
            };
            // 一加
            dict["oneplus"] = new string[] {
                    "com.oneplus.security/.chainlaunch.view.ChainLaunchAppListActivity",
                    "com.oneplus.security"
            };
            // 乐视
            dict["letv"] = new string[] {
                    "com.letv.android.letvsafe/.AutobootManageActivity",
                    "com.letv.android.letvsafe/.BackgroundAppManageActivity",
                    "com.letv.android.letvsafe"
            };
            // 中兴
            dict["zte"] = new string[] {
                    "com.zte.heartyservice/.autorun.AppAutoRunManager",
                    "com.zte.heartyservice"
            };
            // 金立
            dict["f"] = new string[] {
                    "com.gionee.softmanager/.MainActivity",
                    "com.gionee.softmanager"
            };
            // 锤子
            dict["smartisanos"] = new string[] {
                    "com.smartisanos.security/.invokeHistory.InvokeHistoryActivity",
                    "com.smartisanos.security"
            };
            // 360
            dict["360"] = dict["ulong"] = new string[] {
                    "com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity",
                    "com.yulong.android.coolsafe"
            };
            // 联想
            dict["lenovo"] = new string[] {
                    "com.lenovo.security/.purebackground.PureBackgroundActivity",
                    "com.lenovo.security"
            };
            // HTC
            dict["htc"] = new string[] {
                    "com.htc.pitroad/.landingpage.activity.LandingPageActivity",
                    "com.htc.pitroad"
            };
            // 华硕
            dict["asus"] = new string[] {
                    "com.asus.mobilemanager/.MainActivity",
                    "com.asus.mobilemanager"
            };
            // 酷派
            dict["coolpad"] = dict["YuLong"] = new string[] {
                    "com.yulong.android.softmanager/.SpeedupActivity",
                    "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain",
                    "com.yulong.android.security"
            };
            // 努比亚
            dict["nubia"] = new string[] {
                "com.nubia.security/com.nubia.security.autoStart.AutoStartManagerActivity",
                "com.nubia.security/com.nubia.security.autoStart.AutoStartManagerActivity2",
                "com.nubia.security/.autoStart.AutoStartManagerActivity",
                "com.nubia.security/.autoStart.AutoStartManagerActivity2",
                "com.nubia.security/.autoStart.AutoStartSettingsActivity",
                "com.nubia.security",
                "com.nubia.powermaster",
                "com.nubia.powermaster/.ui.PowerMasterActivity",
                "com.nubia.powercenter",
                "com.nubia.powercenter/.ui.PowerCenterActivity",
            };
            // LG
            dict["lg"] = new string[] {
                "com.lge.powermanager/.ui.PwrSavingModeActivity",
                "com.lge.powersavingmode/.ui.PwrSavingModeActivity",
                "com.lge.batterydrainageoptimizer/.ui.BatteryDrainageOptimizerActivity",
                "com.lge.ips/.lgips.LGIPSActivity",
                "com.lge.ips/.lgips.LGIPSMainActivity",
                "com.lge.ips/.lgips.LGIPSActivityForTab",
            };
            // 其他品牌
            dict["other"] = new string[] {
                "com.android.settings/.Settings$BatterySaverSettingsActivity",
                "com.android.settings/.Settings$PowerUsageSummaryActivity",
                "com.android.settings/.Settings$HighPowerApplicationsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRulesActivity",
                "com.android.settings/.Settings$BatterySaverSettingsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleDetailActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleSettingsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleAddRuleActivity",
                "com.android.settings/.Settings$BatterySaverScheduleAddStartTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleAddEndTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleDaySelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRepeatSelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleUpcomingRuleActivity",
                "com.android.settings/.Settings$BatterySaverScheduleSettingsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleListActivity",
                "com.android.settings/.Settings$BatteryOptimizationActivity",
                "com.android.settings/.Settings$BatterySaverSettingsActivity",
                "com.android.settings/.Settings$HighPowerApplicationsActivity",
                "com.android.settings/.Settings$AppAndNotificationBatteryUsageActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleAddStartTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleAddEndTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleDaySelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleRepeatSelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleSettingsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleListActivity",
                "com.android.settings/.Settings$BatterySaverScheduleUpcomingRuleActivity",
                "com.android.settings/.Settings$BatterySaverScheduleSettingsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleAddRuleActivity",
                "com.android.settings/.Settings$BatterySaverScheduleAddStartTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleAddEndTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleDaySelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRepeatSelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleAddStartTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleAddEndTimeActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleDaySelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleRepeatSelectionActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleSettingsActivity",
                "com.android.settings/.Settings$BatterySaverScheduleRuleListActivity",
                "com.android.settings/.Settings$BatterySaverScheduleUpcomingRuleActivity",
                "com.android.settings/.Settings$BatterySaverScheduleSettingsActivity",
            };
			return dict;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232

打开自启动管理界面

        /// <summary>
        /// 打开自启动管理界面
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="Exception"></exception>
        public static void StartAutomaticStartupManagementActivity(Context context)
        {
            List<string> uris = new List<string>();

            // 获取设备的产品型号名称
            var brand = Build.Brand.ToLower();

            // 所有品牌的自启动URI信息
            var dictUri = GetManagerpageUriDictionary();


            // 判断是否获取成功
            if (!string.IsNullOrWhiteSpace(brand) && dictUri.ContainsKey(brand))
            {
                // 保存与产品名称对应的管理界面URI
                uris.AddRange(dictUri[brand]);
            }

            // 保存通用的管理界面URI
            uris.AddRange(dictUri["other"]);

            // 跳转进入品牌对应的自启动管理界面
            foreach (string act in uris)
            {
                try
                {
                    Intent intent;
                    if (act.Contains("/"))
                    {
                        intent = new Intent();
                        intent.AddFlags(ActivityFlags.NewTask);
                        ComponentName componentName = ComponentName.UnflattenFromString(act);
                        intent.SetComponent(componentName);
                    }
                    else
                    {
                        intent = context.PackageManager.GetLaunchIntentForPackage(act);
                    }
                    context.StartActivity(intent);
                    return;
                }
                catch (Exception)
                {
                    Log.Debug("AutomaticStart", $"自启动管理界面路径[{brand}] uri:{act}");
                }
            }

            // 未匹配到有效的自启动管理界面路径
            throw new Exception($"当前设备[{brand}]未匹配到有效的自启动管理界面路径");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

这段代码的作用就是打开设备的自启动管理界面,以便用户可以查看和配置应用程序的自启动权限。它通过使用设备的品牌名称来查找对应的自启动管理界面的 URI 字符串数组,然后依次尝试打开这些 URI,直到成功打开为止。如果没有找到与当前设备品牌匹配的自启动管理界面,则会抛出异常。

结束语

以上,就是跳转到自启动界面的代码。自启动会增加电量的消耗,请妥善使用哦。
如果内容对你有帮助或者使用的URI有错误请留言告诉我。感谢阅读~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/126371
推荐阅读
相关标签
  

闽ICP备14008679号