【问题标题】:How to fix the issue that the activity is killed automatically after some minutes in newly version as like vivo and oppo如何解决vivo和oppo等新版本几分钟后Activity自动终止的问题
【发布时间】:2019-08-08 16:05:31
【问题描述】:

我做了一个应用程序,这是一个按时间间隔存储位置信息并发送到服务器的应用程序 服务在后台或前台模式下工作。在普通的安卓手机上运行良好。 顺便说一句,应用程序在 oppo 和 vivo 中运行不佳 启动第一个活动后,应用程序(此活动)在 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


    【解决方案1】:

    是的,这是因为这些设备默认只允许某些白名单应用的后台服务。如果您的应用也必须这样工作,这意味着您必须从设置中启用 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;
      }
    

    【讨论】:

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

    你不能通过代码做到这一点。您可以为您的应用程序进行手动设置,如下所述,您的应用程序将运行您的后台服务

    应用信息 > 省电模式 > 允许后台运行

    您已选择允许后台运行

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

    在 Realme 1 CPH1859 中测试

    所有手机都没有上述设置

    【讨论】:

      猜你喜欢
      • 2016-02-25
      • 1970-01-01
      • 2018-04-15
      • 2022-07-26
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多