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.

David Buck
  • 3,439
  • 29
  • 24
  • 31
Shivam Nagpal
  • 683
  • 2
  • 9
  • 19
  • 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. – Nishant. Dec 08 '15 at 06:17
  • 17
    @Radix how whatsapp , facebook and many more apps doing this? – Shivam Nagpal Dec 09 '15 at 07:40
  • 6
    I am assuming they have tie ups with Xiaomi to be in the whitelist. Buts its just a guess. – Nishant. Dec 09 '15 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"](http://www.xiaomidev.com/ask-us.html) section, non responsive – Pararth May 11 '16 at 06:20
  • 6
    @ShivamNagpal : Xiaomi whitelists the popular apps like Facebook, Whatsapp, Messenger by default. Others like us have to inform the user to enable it manually. – venkat Sep 13 '16 at 18:51
  • Check out this https://stackoverflow.com/questions/47426313/how-to-fix-the-issue-that-the-activity-is-killed-automatically-after-some-minute/47426982#47426982, this answer is having almost all Intents for most companies phones. – Sachin Varma Feb 16 '18 at 06:13
  • check this link for all devices on earth https://stackoverflow.com/a/58913310/9699754 – Divyanshu Kumar Nov 18 '19 at 11:16
  • @venkat please provide any proof of Xiaomi whitelisting the popular apps, like you said – Johnny Five Dec 19 '19 at 14:15
  • 1
    @JohnnyFive You can install any Facebook company apps like Facebook, WhatsApp or Instagram, then check the whitelisted apps. They will be in the enabled state in the Whitelist screen by default whereas your apps will be disabled by default. – venkat Dec 20 '19 at 15:06

7 Answers7

24

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

ONVETI
  • 3
  • 3
rajkumar
  • 459
  • 4
  • 6
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);
        }
Mohit Mathur
  • 659
  • 8
  • 15
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."));
 }
Daniele Ricci
  • 571
  • 7
  • 27
sreejith
  • 183
  • 1
  • 8
  • 1
    i need to redirect to autostart manager screen in vivo ,"com.iqoo.secure.MainGuideActivity" it is stopping in halfway – user3069590 Oct 31 '17 at 13:26
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

glenn ferns
  • 117
  • 6
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
 }
javisilox
  • 95
  • 9
  • 1
    https://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) – Johnny Five Dec 23 '19 at 08:31
  • Is there even an autostart menu in pure Android like in Miui? – Simon May 13 '21 at 02:42
2
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

Jaffar Raza
  • 301
  • 1
  • 4
  • 19
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

Shaifali Rajput
  • 1,269
  • 9
  • 28
Shashwat Gupta
  • 820
  • 8
  • 19
  • how do solve by asking only once by using this it is asking every time – M.Yogeshwaran Sep 11 '17 at 12:40
  • @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. – Karthi R Nov 09 '17 at 07:09