How to enable auto start for my app in Xiaomi programmatically

后端 未结 7 1256
情话喂你
情话喂你 2020-12-01 01:20

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

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 02:17

    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 list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            if  (list.size() > 0) {
                context.startActivity(intent);
            } 
        } catch (Exception e) {
            Crashlytics.logException(e);
        }
    

提交回复
热议问题