Skip to content Skip to sidebar Skip to footer

How To Fix The Issue That The Activity Is Killed Automatically After Some Minutes In Newly Version As Like Vivo And Oppo

I made a app,This is the app that store the location information by interval and send it to the server Services are working in background or forground mode.It is working well in co

Solution 1:

Yes , this is because these devices only allow background services for some whitelist app by default. If your app also has to work like that means you have to enable autoStart from settings, below code will help you to make user to enable autostart for your app.If autoStart is enabled, your service will work well in background.

privatevoidenableAutoStart() {
    if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
      newMaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(newMaterialDialog.SingleButtonCallback() {
          @OverridepublicvoidonClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

            Intentintent=newIntent();
            intent.setComponent(newComponentName("com.miui.securitycenter",
              "com.miui.permcenter.autostart.AutoStartManagementActivity"));
            startActivity(intent);
          }
        })
        .show();
    } elseif (Build.BRAND.equalsIgnoreCase("Letv")) {
      newMaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(newMaterialDialog.SingleButtonCallback() {
          @OverridepublicvoidonClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

            Intentintent=newIntent();
            intent.setComponent(newComponentName("com.letv.android.letvsafe",
              "com.letv.android.letvsafe.AutobootManageActivity"));
            startActivity(intent);
          }
        })
        .show();
    } elseif (Build.BRAND.equalsIgnoreCase("Honor")) {
      newMaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(newMaterialDialog.SingleButtonCallback() {
          @OverridepublicvoidonClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            Intentintent=newIntent();
            intent.setComponent(newComponentName("com.huawei.systemmanager",
              "com.huawei.systemmanager.optimize.process.ProtectActivity"));
            startActivity(intent);
          }
        })
        .show();
    } elseif (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
      newMaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(newMaterialDialog.SingleButtonCallback() {
          @OverridepublicvoidonClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            try {
              Intentintent=newIntent();
              intent.setClassName("com.coloros.safecenter",
                "com.coloros.safecenter.permission.startup.StartupAppListActivity");
              startActivity(intent);
            } catch (Exception e) {
              try {
                Intentintent=newIntent();
                intent.setClassName("com.oppo.safe",
                  "com.oppo.safe.permission.startup.StartupAppListActivity");
                startActivity(intent);
              } catch (Exception ex) {
                try {
                  Intentintent=newIntent();
                  intent.setClassName("com.coloros.safecenter",
                    "com.coloros.safecenter.startupapp.StartupAppListActivity");
                  startActivity(intent);
                } catch (Exception exx) {

                }
              }
            }
          }
        })
        .show();
    } elseif (Build.MANUFACTURER.contains("vivo")) {
      newMaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
        .content(
          "Please allow QuickAlert to always run in the background.Our app runs in background to detect when you are in distress.")
        .theme(Theme.LIGHT)
        .positiveText("ALLOW")
        .onPositive(newMaterialDialog.SingleButtonCallback() {
          @OverridepublicvoidonClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            try {
              Intentintent=newIntent();
              intent.setComponent(newComponentName("com.iqoo.secure",
                "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
              startActivity(intent);
            } catch (Exception e) {
              try {
                Intentintent=newIntent();
                intent.setComponent(newComponentName("com.vivo.permissionmanager",
                  "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
                startActivity(intent);
              } catch (Exception ex) {
                try {
                  Intentintent=newIntent();
                  intent.setClassName("com.iqoo.secure",
                    "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
                  startActivity(intent);
                } catch (Exception exx) {
                  ex.printStackTrace();
                }
              }
            }
          }
        })
        .show();
    }
  }

  publicbooleancheckServiceRunning() {
    ActivityManagermanager= (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
      Integer.MAX_VALUE)) {
      if ("com.sac.speechdemo.MyService".equals(service.service.getClassName())) {
        returntrue;
      }
    }
    returnfalse;
  }

Solution 2:

You can't do this through code. you can do manual settings for your application as mention below and your app will run your background service

App Info > power saver > Allow Background running

You have yo select Allow Background running

Allowing Auto Start won't work. It will stop background service after some time.

tested in Realme 1 CPH1859

All phones not have above setting

Post a Comment for "How To Fix The Issue That The Activity Is Killed Automatically After Some Minutes In Newly Version As Like Vivo And Oppo"