Android 如何解决vivo和oppo等新版本中活动在几分钟后自动终止的问题

Android 如何解决vivo和oppo等新版本中活动在几分钟后自动终止的问题,android,Android,我制作了一个应用程序,这是一个按时间间隔存储位置信息并将其发送到服务器的应用程序 这些服务在后台或后台模式下运行,在普通android手机上运行良好。 顺便说一句,应用程序在oppo和维梧运行不正常 启动第一个活动后,app(此活动)将在3~5分钟后终止 try { Intent intent = new Intent(); String manufacturer = android.os.Build.MANUFACTURER; if ("xiaomi".equalsIgnoreCase(man

我制作了一个应用程序,这是一个按时间间隔存储位置信息并将其发送到服务器的应用程序 这些服务在后台或后台模式下运行,在普通android手机上运行良好。 顺便说一句,应用程序在oppo和维梧运行不正常 启动第一个活动后,app(此活动)将在3~5分钟后终止

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"));
intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
            }
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
PackageManager.MATCH_DEFAULT_ONLY);
            if  (list.size() > 0) {
                startActivity(intent);
            }
        } catch (Exception e) {
               Crashlytics.logException(e);
            }
        Account account = createSyncAccount(this);
        Intent locationIntent = new Intent(this, UserLocationService.class);
        locationIntent.putExtra("extra.account", account);
        startService(locationIntent);
Intent i = new Intent(this, SynchronizeService.class);
SharedPreferences prefs = 
PreferenceManager.getDefaultSharedPreferences(this);
long synchTime = 
Long.parseLong(prefs.getString(Constants.SETTINGS_SYNCHRONIZE_INTERVAL, "210"));
i.setAction(SynchronizeService.ACTION_SET_ALARM);
if (!UserLocationService.isRepeated)
        i.putExtra(SynchronizeService.EXTRA_TIME, Long.valueOf("30"));
    else i.putExtra(SynchronizeService.EXTRA_TIME, synchTime);
    startService(i);
试试看{
意图=新意图();
字符串制造商=android.os.Build.manufacturer;
if(“小米”。等效信号(制造商)){
setComponent(新组件名称(“com.miui.securitycenter”、“com.miui.permcenter.autostart.AutoStartManagementActivity”);
}else if(“oppo”。等效信号案例(制造商)){
setComponent(新组件名(“com.coloros.safecenter”、“com.coloros.safecenter.permission.startup.StartupAppListActivity”);
}否则,如果(“维梧”。等效信号(制造商)){
//setComponent(新组件名称(“com.vivo.permissionmanager”、“com.vivo.permissionmanager.activity.BgStartUpManagerActivity”);
setComponent(新组件名(“com.iqoo.secure”、“com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity”);
}
List List=getPackageManager().QueryInputActivities(意图,
PackageManager.MATCH_(仅限默认值);
如果(list.size()>0){
星触觉(意向);
}
}捕获(例外e){
Crashlytics.logException(e);
}
Account Account=createSyncAccount(此);
Intent locationIntent=newintent(这是UserLocationService.class);
locationIntent.putExtra(“额外账户”,账户);
startService(位置意向);
Intent i=新Intent(这是SynchronizeService.class);
SharedReferences优先权=
PreferenceManager.GetDefaultSharedReferences(此文件);
长同步时间=
parseLong(prefs.getString(Constants.SETTINGS\u SYNCHRONIZE\u INTERVAL,“210”);
i、 设置动作(同步服务动作设置报警);
如果(!UserLocationService.isRepeated)
i、 putExtra(SynchronizeService.EXTRA_TIME,Long.valueOf(“30”));
else i.putExtra(SynchronizeService.EXTRA_TIME,synchTime);
startService(一);
这是我的资料来源。
你能帮我吗?我没有找到正确的解决方案。如果有人有解决方案,你能告诉我吗?

是的,这是因为默认情况下这些设备只允许为某些白名单应用程序提供后台服务。如果你的应用也必须这样工作,这意味着你必须从设置中启用autoStart,下面的代码将帮助你让用户为你的应用启用autoStart。如果启用了autoStart,你的服务将在后台正常工作

private void enableAutoStart() {
    if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
      new MaterialDialog.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(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 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(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 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(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 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(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 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(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();
    }
  }

  public boolean checkServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
      Integer.MAX_VALUE)) {
      if ("com.sac.speechdemo.MyService".equals(service.service.getClassName())) {
        return true;
      }
    }
    return false;
  }

你不能通过代码来实现这一点。您可以为您的应用程序执行以下手动设置,您的应用程序将运行您的后台服务

应用程序信息>节能器>允许后台运行

您已选择允许后台运行

允许自动启动不起作用。它将在一段时间后停止后台服务

在Realme 1 CPH1859中测试


所有手机都没有上述设置

,但每次启动活动时都会显示该对话框,如果已经启用,则不应显示该对话框。您也可以帮助吗?目前无法检测autostart是否已启用,因此您可以发送一条消息,说明这是强制性的,如果您不允许,服务将不工作,使用SharedReference,您可以设置布尔值检查是否显示服务,我知道这不是一个正确的答案,目前没有其他办法。如果答案对你有帮助,请也投一票。到目前为止,这是最好的答案,但我面临的另一个问题是,如果我在最近的应用程序中按“全部清除”按钮,则服务仍然存在,但当我手动滑动清除应用程序时,服务停止。有什么办法解决它吗?你能在你的服务课上发布一个新问题吗。