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

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

image.png

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

@[toc]

前言

很多 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;
        }

打开自启动管理界面

        /// <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}]未匹配到有效的自启动管理界面路径");
        }

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

结束语

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

相关文章
|
23天前
|
安全 搜索推荐 Android开发
Android vs. iOS:解锁智能手机操作系统的奥秘####
【10月更文挑战第21天】 在当今这个数字化时代,智能手机已成为我们生活中不可或缺的伙伴。本文旨在深入浅出地探讨两大主流操作系统——Android与iOS的核心差异、优势及未来趋势,帮助读者更好地理解这两个平台背后的技术哲学和用户体验设计。通过对比分析,揭示它们如何塑造了我们的数字生活方式,并展望未来可能的发展路径。无论您是技术爱好者还是普通用户,这篇文章都将带您走进一个充满创新与可能性的移动世界。 ####
42 3
|
1月前
|
Ubuntu Linux Android开发
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
本文介绍了如何在Android设备上安装Termux和AnLinux,并通过这些工具运行Ubuntu系统和桌面环境。
124 2
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
|
1月前
|
Web App开发 Android开发
利用firefox调试安卓手机端web
该教程详细介绍如何通过Firefox浏览器实现手机与电脑的远程调试。手机端需安装最新版Firefox,并按指定步骤设置完成;电脑端则需安装15版及以上Firefox。设置完成后,通过工具栏中的“远程调试”选项,输入手机IP地址即可连接。连接确认后,即可使用电脑端Firefox调试器调试手机上的Web信息。注意,调试前手机需提前打开目标网页。
66 2
|
26天前
|
Android开发 iOS开发 UED
安卓与iOS的较量:谁才是智能手机市场的王者?
本文将深入探讨安卓和iOS两大智能手机操作系统之间的竞争关系,分析它们各自的优势和劣势。通过对比两者在市场份额、用户体验、应用生态等方面的表现,我们将揭示出谁才是真正的市场领导者。无论你是安卓粉丝还是iOS忠实用户,这篇文章都将为你提供一些有趣的观点和见解。
|
3月前
|
Android开发
【Azure 环境】记录使用Notification Hub,安卓手机收不到Push通知时的错误,Error_Code 30602 or 30608
【Azure 环境】记录使用Notification Hub,安卓手机收不到Push通知时的错误,Error_Code 30602 or 30608
|
4月前
|
存储 移动开发 Android开发
使用kotlin Jetpack Compose框架开发安卓app, webview中h5如何访问手机存储上传文件
在Kotlin和Jetpack Compose中,集成WebView以支持HTML5页面访问手机存储及上传音频文件涉及关键步骤:1) 添加`READ_EXTERNAL_STORAGE`和`WRITE_EXTERNAL_STORAGE`权限,考虑Android 11的分区存储;2) 配置WebView允许JavaScript和文件访问,启用`javaScriptEnabled`、`allowFileAccess`等设置;3) HTML5页面使用`<input type="file">`让用户选择文件,利用File API;
|
3月前
|
Java Android开发 UED
安卓scheme_url调端:如果手机上多个app都注册了 http或者https 的 intent。 调端的时候,调起哪个app呢?
当多个Android应用注册了相同的URL Scheme(如http或https)时,系统会在尝试打开这类链接时展示一个选择对话框,让用户挑选偏好应用。若用户选择“始终”使用某个应用,则后续相同链接将直接由该应用处理,无需再次选择。本文以App A与App B为例,展示了如何在`AndroidManifest.xml`中配置对http与https的支持,并提供了从其他应用发起调用的示例代码。此外,还讨论了如何在系统设置中管理这些默认应用选择,以及建议开发者为避免冲突应注册更独特的Scheme。
|
5月前
|
网络协议 Android开发 数据安全/隐私保护
Android手机上使用Socks5全局代理-教程+软件
Android手机上使用Socks5全局代理-教程+软件
4697 2
|
6月前
|
监控 安全 Android开发
【新手必读】Airtest测试Android手机常见的设置问题
【新手必读】Airtest测试Android手机常见的设置问题
199 0
|
6月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
107 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用