【问题标题】:HUAWEI kill a background app when the phone is locked手机被锁时华为杀死后台应用
【发布时间】:2018-01-19 21:36:16
【问题描述】:

这是我第一次在这个论坛上提问:p 我制作了一个必须在后台运行的 android 应用程序,即当手机处于待机状态时才能恢复用户的位置。我使用服务和唤醒锁。 该应用程序可以在 SAMSUNG 上运行,但我注意到如果它不在受保护的应用程序列表中,HUAWEI 会杀死该应用程序。 所以我创建了一个对话框来告诉用户激活受保护应用程序列表中的应用程序,如下所示:Keeping a periodic service active while the phone is locked 因为我的应用程序应该在所有安卓手机上运行我想知道是否有其他手机品牌在手机空闲时杀死应用程序请做同样的事情。 提前谢谢你:)

【问题讨论】:

  • 您的服务会调用 startForeground() 吗?

标签: android huawei-mobile-services


【解决方案1】:

我遇到了同样的问题,使用可访问性我试图让类名或活动名传入意图并打开设置,看起来旧的受保护的应用程序列表不再可用,a新方法是:

在华为启动禁用应用

在奥利奥华为 p10 上测试:

对于手动,您可以执行以下步骤:

设置 -> 电池 -> 启动

找到您的应用程序并禁用

以编程方式:

public class Constant {


    public static List<Intent> POWERMANAGER_INTENTS = Arrays.asList(
            new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
            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.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.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")).setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart"))
    );
}

将下面的代码放在你的 Util 或 Activity 类中

 private static boolean isCallable(Context context, Intent intent) {
      List<ResolveInfo>list=context.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

saveUserSessionManager 是一个 Preference,你可以设置你的偏好类而不是 SaveUserSessionManager

public static void startPowerSaverIntent(Context context, SaveUserSessionManager saveUserSessionManager) {
    boolean skipMessage = saveUserSessionManager.getDataByKey("skipProtectedAppCheck", false);

    if (!skipMessage) {
        boolean foundCorrectIntent = false;
        for (Intent intent : Constant.POWERMANAGER_INTENTS) {
            if (isCallable(context, intent)) {
                foundCorrectIntent = true;


                new AlertDialog.Builder(context)
                        .setTitle(Build.MANUFACTURER + " Protected Apps")
                        .setMessage(String.format("%s requires to be 'White list' to function properly.\nDisable %s from list.%n", context.getString(R.string.app_name), context.getString(R.string.app_name)))
                        .setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                context.startActivity(intent);
                                saveUserSessionManager.storeDataByKey("skipProtectedAppCheck", true);
                                dialog.dismiss();
                            }
                        })
                        .show();
                break;
            }
        }
    }
}

如何打电话? 在 onResume 方法中的 MainActivity 中检查它是否启用。

 @Override
protected void onResume() {
    super.onResume();

    //saveUserSessionManager is just a Preference you can set your preference class instead of SessionManager

    if (!saveUserSessionManager.getDataByKey("skipProtectedAppCheck", false)) {

        Utils.startPowerSaverIntent(mContext, saveUserSessionManager);
    }
}

就是这样:)

【讨论】:

  • 似乎是一个不错的解决方案...关于在 Ionic 应用程序中实现此功能的任何想法?
【解决方案2】:

持有唤醒锁可能不是你想要的,因为这并不一定能保护你的进程不被杀死。提高应用进程优先级的最佳方法是确保 Service 在前台运行。

在 Android O 上,您可以通过startForegroundService(Intent) 实现这一功能的新方法

这将在通知托盘中放置一条通知,通知 Android 操作系统您的进程当前正在运行。完成任务后,请务必停止服务。

有关Services 的更多信息,我会查看the Service documentation。这将向您展示如何使用正确的文本添加通知。

查看Background Process documentation,了解 Android O 中后台进程的工作方式也可能会有所帮助。如果您正确地遵循了这一点(并从前台开始),那么您的流程应该尽可能地适应所有制造商。

【讨论】:

    【解决方案3】:

    我正在开发的应用程序需要在后台运行并为此使用前台服务。从 Android 8+ 开始(可能不仅是这些版本),许多华为用户报告说应用程序正在关闭(W/O 任何通知)6 屏幕被锁定后 ​​7 分钟(我没有看到 here 和 @ 987654322@).

    确实,华为设备默认会终止正在运行的应用程序以优化电池消耗(很酷吧?)。 AFAIK 无法以编程方式解决此问题。许多开发人员建议用户如何将应用程序列入白名单(例如:Endomondo)。 我还在寻找一种以编程方式检测优化的方法,以便至少警告用户。 Here你可以找到一个可能的解决方案,但我还没有时间试一试。

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      相关资源
      最近更新 更多