How to start Power Manager of all android manufactures to enable background and push notification?

22,323

Solution 1

i have collected some intent from various post:

    private static final Intent[] POWERMANAGER_INTENTS = {
            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.startupmgr.ui.StartupNormalAppListActivity")),
            new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
            new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
            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.samsung.android.lool", "com.samsung.android.sm.battery.ui.BatteryActivity")),
            new Intent().setComponent(new ComponentName("com.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity")),
            new Intent().setComponent(new ComponentName("com.htc.pitroad", "com.htc.pitroad.landingpage.activity.LandingPageActivity")),
            new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.MainActivity")),
            new Intent().setComponent(new ComponentName("com.transsion.phonemanager", "com.itel.autobootmanager.activity.AutoBootMgrActivity"))
    }; 

            for (Intent intent : POWERMANAGER_INTENTS)
                if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
                    // show dialog to ask user action
                    break;
                }

after user agree

for (Intent intent : POWERMANAGER_INTENTS)
  if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
  startActivity(intent);
  break;
}

seams new releases require this permissions

<uses-permission android:name="oppo.permission.OPPO_COMPONENT_SAFE"/>
<uses-permission android:name="com.huawei.permission.external_app_settings.USE_COMPONENT"/>

i want to collect all intent to open power manager, if anyone found mistake or want to improve something, comment here

Solution 2

Try this code-:

private void enableAutoStart() {
    if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
      new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow AppName to always run in the background,else our services can't be accessed.")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(new MaterialDialog.SingleButtonCallback() {
          @Override
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.miui.securitycenter",
              "com.miui.permcenter.autostart.AutoStartManagementActivity"));
            startActivity(intent);
          }
        })
        .show();
    } else if (Build.BRAND.equalsIgnoreCase("Letv")) {
      new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow AppName to always run in the background,else our services can't be accessed.")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(new MaterialDialog.SingleButtonCallback() {
          @Override
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.letv.android.letvsafe",
              "com.letv.android.letvsafe.AutobootManageActivity"));
            startActivity(intent);
          }
        })
        .show();
    } else if (Build.BRAND.equalsIgnoreCase("Honor")) {
      new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow AppName to always run in the background,else our services can't be accessed.")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(new MaterialDialog.SingleButtonCallback() {
          @Override
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.huawei.systemmanager",
              "com.huawei.systemmanager.optimize.process.ProtectActivity"));
            startActivity(intent);
          }
        })
        .show();
    } else if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
      new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow AppName to always run in the background,else our services can't be accessed.")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(new MaterialDialog.SingleButtonCallback() {
          @Override
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            try {
              Intent intent = new Intent();
              intent.setClassName("com.coloros.safecenter",
                "com.coloros.safecenter.permission.startup.StartupAppListActivity");
              startActivity(intent);
            } catch (Exception e) {
              try {
                Intent intent = new Intent();
                intent.setClassName("com.oppo.safe",
                  "com.oppo.safe.permission.startup.StartupAppListActivity");
                startActivity(intent);
              } catch (Exception ex) {
                try {
                  Intent intent = new Intent();
                  intent.setClassName("com.coloros.safecenter",
                    "com.coloros.safecenter.startupapp.StartupAppListActivity");
                  startActivity(intent);
                } catch (Exception exx) {

                }
              }
            }
          }
        })
        .show();
    } else if (Build.MANUFACTURER.contains("vivo")) {
      new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow AppName to always run in the background.Our app runs in background else our services can't be accesed.")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(new MaterialDialog.SingleButtonCallback() {
          @Override
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            try {
              Intent intent = new Intent();
              intent.setComponent(new ComponentName("com.iqoo.secure",
                "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
              startActivity(intent);
            } catch (Exception e) {
              try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                  "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
                startActivity(intent);
              } catch (Exception ex) {
                try {
                  Intent intent = new Intent();
                  intent.setClassName("com.iqoo.secure",
                    "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
                  startActivity(intent);
                } catch (Exception exx) {
                  ex.printStackTrace();
                }
              }
            }
          }
        })
        .show();
    }
  }

Solution 3

If have a problem with a specific OEMs, please open an issue on the Android issuetracker as this maybe a CDD violation. Google can contact the OEM and request that they fix the ROM. This is going to take time, in the meanwhile, you can take a look at sites like don't kill my app to understand what are the constraints on a specific device and use a library like autostarter to help the user to navigate to the right setting.

Share:
22,323
Alessandro Scarozza
Author by

Alessandro Scarozza

android developer

Updated on February 25, 2021

Comments

  • Alessandro Scarozza
    Alessandro Scarozza about 3 years

    Some Android devices due to custom Android tweaks are done by manufacturers has some politics about Power Management that breaks some features like push notifications.

    • Huawei - Only Pre-EMUI 5.0 / Android 7 - Go to Settings > "Protected apps", check your app.
    • Sony - Tap on the battery icon. Go to Power Management > STAMINA mode > Apps active in standby > Add your app.
    • Asus - Check your app in the Auto-start Manager.
    • Xiaomi - Security (App) > Permissions > Autostart - Enable your app
    • *New Xiaomi - Settings > Developer Options. Disable "memory optimization". To enabled Developer Options go to Settings > About. Tap on MIUI 8 times.
    • Oppo - Go to Settings > "Security settings" > "Data saving" and enable your app.
    • Samsung - Disable battery usage optimizations

    I want to collect intents to launch respective tools, but I have found only for Huawei and Xiaomi.

    Intent INTENT_HUAWEI = new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
    Intent INTENT_XIAOMI = new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
    
    if (getPackageManager().resolveActivity(INTENT_HUAWEI, PackageManager.MATCH_DEFAULT_ONLY) != null)
        startActivity(INTENT_HUAWEI);
    else if (getPackageManager().resolveActivity(INTENT_XIAOMI, PackageManager.MATCH_DEFAULT_ONLY) != null)
        startActivity(INTENT_XIAOMI);
    

    I need help from all other producers, thz

  • Shivam Oberoi
    Shivam Oberoi over 6 years
    You can add in your desired band Build.MANUFACTURER.equalsIgnoreCase("oppo")
  • Alessandro Scarozza
    Alessandro Scarozza over 6 years
    any help about intent for ASUS ?
  • drk
    drk about 6 years
    Looks like we try to achieve the same thing. Can you please check this Github repo? github.com/dirkam/backgroundable-android
  • Alessandro Scarozza
    Alessandro Scarozza about 6 years
    yes, i have collected more intents, you have collected more details. on xiaomi we have found different intent
  • drk
    drk about 6 years
    I believe Github is a better platform for this, would you mind contributing? Still, a lot of other OEMs to cover, maybe others can help, too.
  • Markus Penguin
    Markus Penguin about 6 years
    Thank you! Using your information I created a utility class for showing this dialog.
  • kilian eller
    kilian eller about 6 years
    would you updated this? is it still up to date for Samsung?
  • Alessandro Scarozza
    Alessandro Scarozza about 6 years
    @kilianeller more or less. if anyone post a comment, i will update answer
  • hoshiKuzu
    hoshiKuzu almost 6 years
    Huawei removed its "protected apps" in recent versions of EMUI, in favour of a new activity com.huawei.systemmanager.appcontrol.activity.StartupAppContr‌​olActivity.
  • Jule
    Jule almost 6 years
    For HTC, there's Boost+: com.htc.pitroad/.landingpage.activity.LandingPageActivity - then select "manage background apps" (or similar).
  • Alessandro Scarozza
    Alessandro Scarozza almost 6 years
    @Jule i have no htc now, pls make and test an intent and i will add in main post. somethings like new Intent().setComponent(new ComponentName("com.htc.pitroad", "com.htc.pitroad.landingpage.activity.LandingPageActivity")) pls test
  • Jule
    Jule almost 6 years
    @Xan yes precisely that intent works, tested it. And I found out by now that the Boost+ app is also available in the play store, so not limited to HTC devices (although those come with it pre-installed).
  • Alessandro Scarozza
    Alessandro Scarozza almost 6 years
    @Jule added to main post
  • Thom
    Thom almost 6 years
    Hi all , Thanks for the list , But how we can find user make my app to stop running or mark the app under sleeping apps ?
  • Alessandro Scarozza
    Alessandro Scarozza almost 6 years
    simple you can't
  • hoshiKuzu
    hoshiKuzu about 5 years
    The Huawei intent from my older comment no longer works on pie. It crashes with a SecurityException.
  • Alessandro Scarozza
    Alessandro Scarozza about 5 years
    i cant try now, if you find solution i will update post
  • Saeed Arianmanesh
    Saeed Arianmanesh about 5 years
    What is the new huawei battery settings (App launch) intent in android 9 Pie?
  • Himanshu
    Himanshu about 5 years
    I am getting crash with this log :::::::java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.coloros.safecenter/.startupapp.StartupAppListActivit‌​y } from ProcessRecord{7bca5d9 20179:com.appname/u0a230} (pid=20179, uid=10230) requires oppo.permission.OPPO_COMPONENT_SAFE
  • Alessandro Scarozza
    Alessandro Scarozza about 5 years
    so using "oppo.permission.OPPO_COMPONENT_SAFE" permission works ?
  • Ton
    Ton about 5 years
    New Huawei P30 Pro (VOG-L29) using Android Pie displays error "permission USE_COMPONENT is required". So I think we have to add the line <uses-permission android:name="com.huawei.permission.external_app_settings.US‌​E_COMPONENT"/> in manifest.
  • Alon Minski
    Alon Minski almost 5 years
    I added these permissions and code... but I think that on Huawei running Android 9, these permissions do not register. I still have a securityException. Anyone else encountered this?
  • David Sucharda
    David Sucharda almost 5 years
    I must say that adding "com.huawei.permission.external_app_settings.USE_COMPONENT" permission does not work on my Huawei P20 (AN Pie) but I found out that I can use ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppList‌​Activity") instead.
  • Alessandro Scarozza
    Alessandro Scarozza almost 5 years
    @DavidSucharda added in list
  • AlexS
    AlexS almost 5 years
    @DavidSucharda this is so hard for users, i think... but no any other ways exist. Thank you.
  • David Sucharda
    David Sucharda almost 5 years
    @AlexS It is, I completely agree. Problem is that Huawei for example kills every background activity after 12 seconds of device inactivity. Not even Foreground service is spared, which is crazy. Other thing is that sometimes it is "disable" optimization and sometimes "enable" manual optimization. So you can mislead the user with your wording in the application.
  • Sethuraman Srinivasan
    Sethuraman Srinivasan almost 5 years
    @AlonMinski Did you resolve that security exception issue?
  • Sethuraman Srinivasan
    Sethuraman Srinivasan almost 5 years
    @himCream Did you resolve that security exception issue?
  • Alon Minski
    Alon Minski almost 5 years
    @SethuramanSrinivasan yes. I launched a different activity in Android 9: new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppList‌​Activity"))
  • itay83
    itay83 over 4 years
    StartupNormalAppListActivity should be first unless it crashes (security exception).
  • Chris Watts
    Chris Watts about 4 years
    This Gist is a working example, but beware of SecurityExceptions as mentioned in the other comments: gist.github.com/moopat/e9735fa8b5cff69d003353a4feadcdbc
  • Elroid
    Elroid over 3 years
    Note at some point samsung changed the activity name from com.samsung.android.sm.ui.battery.BatteryActivity to com.samsung.android.sm.battery.ui.BatteryActivity . See my response to a related question here: stackoverflow.com/a/63632966/621465
  • beginner
    beginner about 3 years
    For the latest Samsung devices use com.samsung.android.sm.battery.ui.BatteryActivity instead of com.samsung.android.sm.ui.battery.BatteryActivity
  • Alessandro Scarozza
    Alessandro Scarozza about 3 years
    aggiornato il post
  • Lynch Chen
    Lynch Chen about 3 years
    I've tested this on Oppo A1601 running Android 6.0, adding oppo.permission.OPPO_COMPONENT_SAFE still cause an exception when starting com.coloros.safecenter.startupapp.StartupAppListActivity, currently working alternative is adding it's parent activity com.coloros.privacypermissionsentry.PermissionTopActivity and let users click inside by themselves. This solution is referred from https://stackoverflow.com/questions/49814755/denial-permissi‌​on-oppo-component-sa‌​fe
  • Lynch Chen
    Lynch Chen about 3 years
    I've tested the above codes on some newer SAMSUNG, OnePlus phones running android 10+, seems like com.samsung.android.sm.ui.battery.BatteryActivity doesn't have a whitelist page for apps anymore, they're acting more like an AOSP. Opening the battery page of SAMSUNG doesn't help in this case, I can't find any place to whitelist my app. In this condition, I suggest separate PowerSaving and Autostart, for PowerSaving thing, first check existence&start new Intent().setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATI‌​ON_SETTINGS) , if not exist, then open vendor-specific battery activity.
  • Devdatta
    Devdatta over 2 years
    Hi @Xan first Thanks For this solution, for infinix device what should we use?
  • Alessandro Scarozza
    Alessandro Scarozza over 2 years
    @Devdatta i don't know, if you find something tell me, i will add on list :)