How to enable auto start for my app in xiaomi prog

2019-01-13 06:26发布

I have service in my app which need to be running in background all the time , in all devices its working fine except Xiaomi , i have read some where that we need to enable auto start in settings for app to keep service running.

So please tell me how o enable auto start pro grammatically because user will never do that.

Any help will be appreciated.

5条回答
家丑人穷心不美
2楼-- · 2019-01-13 07:00

you can do it by following:

      if (manufactXiaomi.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
        if (!session.getVisibilityOfAutoStartDialog()) {Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
            startActivity(intent);}}

if you want to keep running your service is the background you need to change some setting of your device Check This

might above code works for you

查看更多
等我变得足够好
3楼-- · 2019-01-13 07:01

You may try this:

if ("xiaomi".equalsIgnoreCase(str)) 
{
 intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                    } 
else if ("oppo".equalsIgnoreCase(str)) 
{
 intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
                    } 
else if ("vivo".equalsIgnoreCase(str)) 
{
  intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.MainGuideActivity."));
 }
查看更多
祖国的老花朵
4楼-- · 2019-01-13 07:03

I stumbled upon this library. Autostarter. It is a autostarter library for different device manufacturers

Last time i used it, it had support for Xiaomi and Letv devices. I cant really give you code examples, but i hope it helps someone who stumbles upon this

查看更多
仙女界的扛把子
5楼-- · 2019-01-13 07:09

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);
    }
查看更多
不美不萌又怎样
6楼-- · 2019-01-13 07:09

Try this...it's working for me. It will open the screen to enable autostart. But if you try to disable from there it will close the app. I am figuring out a solution for that. Till then you can use this as solution.

String manufacturer = "xiaomi";
        if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            //this will open auto start screen where user can enable permission for your app
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
            startActivity(intent);
        }
查看更多
登录 后发表回答