如何以编程方式在小米中为我的应用程序启用自动启动 [英] How to enable auto start for my app in Xiaomi programmatically

查看:124
本文介绍了如何以编程方式在小米中为我的应用程序启用自动启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一项服务,该服务需要一直在后台运行.在所有设备上,除小米外,其他设备均能正常工作.我读过某个地方,我们需要在应用程序中启用自动启动设置以保持服务运行.

I have a service in my app which needs to be running in the background all the time. On all devices, it's working fine except Xiaomi. I have read somewhere that we need to enable auto-start in settings for an app to keep a service running.

因此,请告诉我如何以编程方式启用自动启动,因为用户将永远不会这样做.

So please tell me how to enable auto-start programmatically, because the user will never do that.

推荐答案

您不能直接启用自动启动功能,但可以将用户重定向到自动启动设置屏幕,并要求用户为您的应用程序将其打开.将以下解决方案用于小米,oppo和vivo手机.自动启动屏幕将启动(如果存在).

You cannot enable auto start directly, but you can redirect user to auto start setting screen and ask user to turn it on for your app. Use the below solution for xiaomi, oppo and vivo phones. Autostart screen will be launched if it exists.

    try {
        Intent intent = new Intent();
        String manufacturer = android.os.Build.MANUFACTURER;
        if ("xiaomi".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
        } else if ("oppo".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
        } else if ("vivo".equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
        }

        List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if  (list.size() > 0) {
            context.startActivity(intent);
        } 
    } catch (Exception e) {
        Crashlytics.logException(e);
    }

这篇关于如何以编程方式在小米中为我的应用程序启用自动启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆