android - NotificationListenerService 和 Doze 模式以及 App Standby

标签 android android-notifications wear-os android-doze android-appstandby

我有一个应用程序可以监听电话通知并通过 MessageApi 向 Android Wear watch 发送消息。除了某些搭载 Android 6 的设备,尤其是 Huawei Mate 8(看起来所有 Huawei Android 6 都这样做)之外,一切都很好。

华为有自己的卡住应用程序后台处理( protected 应用程序)的实现。从用户报告中,我已经确认我的应用程序在华为 protected 应用程序和 Android 6 的 Doze 模式中存在异常。该应用程序运行正常,但在显示屏关闭整整 15 分钟后,我的应用程序停止向连接的 Android Wear watch 发送消息。我的应用程序还可以记录收到的通知历史记录,15 分钟后什么也没有收到……直到手机显示屏打开并打开我的应用程序。之后,所有本应在手机显示屏关闭时到达的通知都到达了我的 NotificationListenerService 实现,并一次性发送到 watch 。有记载的历史也证实了这一点。

有什么想法可以解决这些手机的这个问题,尤其是 Huawei Mate 8 with Android 6 with Doze mode?

当设备处于休眠模式和/或应用处于待机模式时,NotificationListenerService 的正确行为是什么?

编辑

用户还确认他们的手机没有处于省电模式,这也会影响后台应用程序及其服务。这个错误看起来像是华为独有的,因为没有 Nexus 用户报告过这个,我的 OnePlus One with M 也没有这样做。 N 预览也适用于 Nexus 设备。

编辑 2

我添加了一个可选的前台服务 (startForeground()),所以我的应用程序在通知中心有一个永久通知,因此我的应用程序应该被排除在每次电池优化之外。对于前台服务通知,我使用了 NotificationCompat.PRIORITY_MIN 的优先级并添加了 Notification.FLAG_ONGOING_EVENT 标志。这对华为手机有一点帮助,但作用不大,现在延迟通知会在屏幕打开后立即到达我的 NotificationListenerService,而不是在打开我的应用程序后。我没有在我的 NotificationListenerService 中使用 startForeground(),而是在另一个 Service 中使用,因为我无法控制它的生命周期。

最佳答案

对于华为设备(不确定是否适用于所有华为设备),您需要请求 protected 应用程序权限,以便您的应用程序在进入后台时不会被卡住。

检测华为设备是否有Protected Apps权限:

private boolean hasProtectedAppsSetting() {
    Intent intent = new Intent();
    intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");

    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

调出华为保护应用设置页面:

private void showProtectedAppsSetting() {
    try {
        String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            cmd += " --user " + getUserSerial();
        }
        Runtime.getRuntime().exec(cmd);
    } catch (IOException ignored) {
    }
}

private String getUserSerial() {
    //noinspection ResourceType
    Object userManager = getSystemService("user");
    if (null == userManager) return "";

    try {
        Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
        Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
        Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
        long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
        return String.valueOf(userSerial);
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException ignored) {
    }
    return "";
}

不幸的是,我没有找到任何方法来检查用户是否已将您的应用程序授予 protected 应用程序。如果有人知道请分享:)

引用: http://ndroid.info/ldquo_protected_appsrdquo_setting_on_huawei_phones_and_how_to_handle_it

关于android - NotificationListenerService 和 Doze 模式以及 App Standby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36900480/

相关文章:

Android Proguard 在使用 AVG Antivirus 时遇到一些问题

android - 服务中的前台通知在 Android 8.1 中不起作用

android通知点击不工作

java - 如何获取表盘复杂功能的提供者信息?

javascript - Android 上的手机唤醒后 HTML5 GPS 地理定位失败

android - SurfaceTexture 方法并不总是在 Android 7.0 上调用

android - Kotlin-allopen for android

java - 警报通知未出现

android - android wear 中的 WebView 在运行时出错

android - 获取 WearableListView 的位置