在中国手机中向上滑动时应用后台服务停止

4 java android background-service oppo huawei-mobile-services

我一直在寻找有关中国手机(Oppo、华为、小米、Vivo 等)在向上滑动(关闭)应用程序时,后台服务停止运行时出现此问题的答案。

大多数解决方案是:

  1. 包含 START_STICKY 并使用 AlarmManager 启动服务。
  2. 以编程方式将用户引导至自动启动管理器以按用户启用应用程序。
  3. 手动从省电模式中排除我的应用程序或将我的应用程序包含为受保护的应用程序。

我的问题是像 Whatsapp 这样的应用程序如何仍然接收消息或通知甚至向上滑动?此外,1.和2.中提到的解决方案如果手机重新启动就不起作用,但是Whatsapp仍然可以接收消息?我已经测试过三星设备,即使应用程序被向上滑动,它们在运行后台服务时也没有问题。有没有人在中国手机上遇到同样的问题?

小智 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"));
            } else if ("oneplus".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.oneplus.security", "com.oneplus.security.chainlaunch.view.ChainLaunchAppListAct??ivity"));
            } else if ("Letv".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
            } else if ("Honor".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
            }
            else if ("huawei".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
            }
            else if ("asus".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.asus.mobilemanager","com.asus.mobilemanager.autostart.AutoStartActivity"));
            }
            else {
                Log.e("other phone ", "===>");
            }
            List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            if (list.size() > 0) {
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

  • 华为设备不再支持 **com.huawei.systemmanager.optimize.process.ProtectActivity** 将其更改为 **com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity** (2认同)

Fai*_*ikh 0

我仍在寻找答案。我已联系 Google 支持团队,他们的回复如下:

经过与我们的工程师讨论,这确实是预期的行为。因为他们使用的库存 ROM 禁止大多数应用程序重新启动后台服务。用户应手动启用后台服务的自动启动,因为默认情况下这些服务是禁用的。无法以编程方式为所有设备启用此功能。因此,您必须提示用户手动执行这些步骤。请查看本指南以了解有关此解决方法的更多信息。