27

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.

10
  • 5
    you wont be able to do that from code until and unless there is some api from xiaomi that gives you access to that functionality. I am guessing auto start manager is an app( with privileges of that of a system app, since part of xiaomi modified os) , hence not possible. On the other-hand if the device is rooted you can actually disable autostart manager. Commented Dec 8, 2015 at 6:17
  • 18
    @Radix how whatsapp , facebook and many more apps doing this? Commented Dec 9, 2015 at 7:40
  • 7
    I am assuming they have tie ups with Xiaomi to be in the whitelist. Buts its just a guess. Commented Dec 9, 2015 at 17:59
  • 1
    agree with @Radix on the guess part, which seems ugly, from xiaomi, for app development generally. Is there any update on the question? Also, i checked "xiaomi devs ask us" section, non responsive Commented May 11, 2016 at 6:20
  • 7
    @ShivamNagpal : Xiaomi whitelists the popular apps like Facebook, Whatsapp, Messenger by default. Others like us have to inform the user to enable it manually. Commented Sep 13, 2016 at 18:51

7 Answers 7

29

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, vivo, letv, honor, asus, nokia, huawei phones. Autostart screen will be launched if it exists.

 try { val intent = Intent() val manufacturer = Build.MANUFACTURER when { "xiaomi".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity" ) } "oppo".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity" ) } "vivo".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity" ) } "letv".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity" ) } "honor".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity" ) } "asus".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.asus.mobilemanager", "com.asus.mobilemanager.powersaver.PowerSaverSettings" ) } "nokia".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.evenwell.powersaving.g3", "com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity" ) } "huawei".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity" ) } } startActivity(intent) } catch (e: Exception) { /*Timber.e(e)*/ } 

Quvonchbek Y answered

Sign up to request clarification or add additional context in comments.

1 Comment

the activity name for vivo is incorrect. maybe they change it from version to version.
7

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); } 

5 Comments

Mohit every time it is asking how do you filter that ask only once is there any callback method for this.
@M.Yogeshwaran callback is not there since I am explicitly trying to call the activity.
how can i call i only once every time is asking the permission
you can also check this link stackoverflow.com/questions/41277671/…
mine is still killing the floating window service any solution?
3
import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Build; import android.support.v7.widget.AppCompatCheckBox; import android.widget.CompoundButton; import java.util.List; public class Utils { public static void startPowerSaverIntent(Context context) { SharedPreferences settings = context.getSharedPreferences("ProtectedApps", Context.MODE_PRIVATE); boolean skipMessage = settings.getBoolean("skipProtectedAppCheck", false); if (!skipMessage) { final SharedPreferences.Editor editor = settings.edit(); boolean foundCorrectIntent = false; for (Intent intent : Constants.POWERMANAGER_INTENTS) { if (isCallable(context, intent)) { foundCorrectIntent = true; final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(context); dontShowAgain.setText("Do not show again"); dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean("skipProtectedAppCheck", isChecked); editor.apply(); } }); new AlertDialog.Builder(context) .setTitle(Build.MANUFACTURER + " Protected Apps") .setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", context.getString(R.string.app_name))) .setView(dontShowAgain) .setPositiveButton("Go to settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { context.startActivity(intent); } }) .setNegativeButton(android.R.string.cancel, null) .show(); break; } } if (!foundCorrectIntent) { editor.putBoolean("skipProtectedAppCheck", true); editor.apply(); } } } private static boolean isCallable(Context context, Intent intent) { List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; } } } import android.content.ComponentName; import android.content.Intent; import java.util.Arrays; import java.util.List; public class Constants { public static List<Intent> POWERMANAGER_INTENTS = Arrays.asList( new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")), new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")), new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")), new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")), new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")), new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")), new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")), new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")), new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")), new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")).setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart")) ); } 

Reference Answer

Comments

2

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.")); } 

1 Comment

i need to redirect to autostart manager screen in vivo ,"com.iqoo.secure.MainGuideActivity" it is stopping in halfway
2

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

Comments

2

@rajkumar is right, but you have to take care with Xiaomi running pure Android, MI A1, MI A2 and MI A3 is coming, so you should add something like:

if (Build.MANUFACTURER.toLowerCase().contains("xiaomi") && !Build.MODEL.toLowerCase().contains("mi a")){ //intent } 

2 Comments

gist.github.com/Muyangmin/e8ec1002c930d8df3df46b306d03315d @javisilox This can detect whether device has MIUI, therefore can solve problem you described in a more general and precise way. (Tested on: MI A2 lite, Redmi 5 plus)
Is there even an autostart menu in pure Android like in Miui?
0

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

2 Comments

how do solve by asking only once by using this it is asking every time
@M.Yogeshwaran Programmatically we can't check the auto-start status. 1. Have one permanent setting for "Enable autostart" in ur App setting screen. 2.And show a dialog/card at your Home screen with dismiss option. And finally do this check only for the devices have this Auto-start problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.