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有错误请留言告诉我。感谢阅读~

相关文章
|
30天前
|
Android开发 开发者 iOS开发
APP开发后如何上架,上架Android应用市场前要准备什么
移动应用程序(APP)的开发已经成为现代企业和开发者的常见实践。然而,开发一个成功的APP只是第一步,将其上架到应用商店让用户下载和使用是实现其潜力的关键一步。
|
27天前
|
JavaScript Android开发
手机也能搭建个人博客?安卓Termux+Hexo搭建属于你自己的博客网站
手机也能搭建个人博客?安卓Termux+Hexo搭建属于你自己的博客网站
20 0
|
2天前
|
安全 Android开发
如何在Android手机上安装第三方应用?
【2月更文挑战第4天】在Android系统中,安装第三方应用是一个常见的需求。本文将介绍如何在Android手机上安装第三方应用,并提供详细的步骤和注意事项。
9 2
|
5天前
|
传感器 人工智能 监控
Java智慧工地管理云平台源码 带AI识别、桌面管理+大屏指挥+手机APP
智慧工地平台支持项目级、公司级、集团级多级权限划分,可根据企业的组织架构进行项目权限、功能权限、数据权限设定。
24 0
|
7天前
|
安全 Java 数据挖掘
当 App 有了系统权限,真的可以为所欲为? Android Performance Systrace
当 App 有了系统权限,真的可以为所欲为? Android Performance Systrace 转载自: https://androidperformance.com/2023/05/14/bad-android-app-with-system-permissions/#/0-Dex-%E6%96%87%E4%BB%B6%E4%BF%A1%E6%81%AF
21 0
|
21天前
|
数据安全/隐私保护 Android开发 iOS开发
苹果APP安装包ipa如何安装在手机上
苹果APP的安装比安卓复杂且困难,很多人不知道如何将ipa文件安装到手机上。以下是几种苹果APP安装在iOS设备的方式,供大家参考。
|
1月前
|
Android开发
闲暇时间收集和整理的Android的一些常用的App
闲暇时间收集和整理的Android的一些常用的App
10 0
|
1月前
|
人工智能 前端开发 JavaScript
APP开发毕业设计|ssm爱心小屋公益机构智慧管理APP(二)
APP开发毕业设计|ssm爱心小屋公益机构智慧管理APP
APP开发毕业设计|ssm爱心小屋公益机构智慧管理APP(二)
|
1月前
|
测试技术 应用服务中间件 定位技术
APP开发毕业设计|ssm爱心小屋公益机构智慧管理APP(一)
APP开发毕业设计|ssm爱心小屋公益机构智慧管理APP
|
1月前
|
Android开发 UED 开发者
解释Android App Bundle是什么,它的优势是什么?
解释Android App Bundle是什么,它的优势是什么?
28 0

相关产品

  • 云迁移中心