【问题标题】:Android 11 - foregroundService's onTaskRemoved is trigged when home button pressedAndroid 11 - 按下主页按钮时触发前台服务 onTaskRemoved
【发布时间】:2021-01-19 08:21:51
【问题描述】:

我在 Pixel 4XL (Android 11) 上看到一些奇怪的行为。 我的前台服务的 onTaskRemoved 被意外调用。 这不会发生在任何其他设备上,但我没有任何其他 Android 11 设备并且模拟器无法执行 BLE。

我的应用使用正在进行的 BLE 连接与另一台非 Android 设备进行通信。 前台服务用于确保程序保持活动状态以接收来自设备的 BLE 通信。

这并不总是发生,但在大多数情况下(75% 的时间),在按下主页(在 Pixel 4XL 上从屏幕底部向上滑动)后,这将导致调用前台服务的 onTaskRemoved。

在我的应用中打开不同的活动(即 MainActivity 以外的活动)几乎可以保证会发生这种情况。

从通知栏打开设置然后刷回家仍然会触发这种情况发生,所以这不可能是我不小心杀死了该应用,因为我在设置应用处于焦点时向上滑动。

据我了解,这个方法应该只在用户通过在任务切换器中滑动来终止应用程序时触发。

如果我返回任务切换器,在服务被终止后,我的应用程序仍然可用并显示最后打开的活动及其状态。切换到它会恢复到正确的位置,因此应用程序本身一定没有被终止。 手机已插入充电,因此不应具有打瞌睡/睡眠功能。 这可能会在启动应用后 60 秒内发生,而不是 30 分钟。

这里到底发生了什么? 我唯一能想到的是,由于某种原因,它不被视为前台服务。在 onTaskRemoved 时或前后,logcat 中没有错误。我没有在代码中的任何地方调用 onTaskRemoved。

我已尝试关注 dumpsys in this post,但它似乎有所不同,我找不到提到的任何参考资料。

通知本身是这样列出的:

$ adb shell dumpsys activity services | grep foreground
isForeground=true foregroundId=13459 foregroundNoti=Notification(channel=xxxService shortcut=null contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x62 color=0x00000000 actions=1 vis=PRIVATE)

是的,我在清单中设置了前台权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

BLEService onCreate 方法(删除了一些不相关的):

@Override
public void onCreate()
{
    BLEManager.getInstance(getApplicationContext());

    startForeground(
            NotificationHandler.NOTIFICATION_BTCONNECTION_ID,
            NotificationHandler.getInstance(getApplicationContext()).createConnectionNotification(getApplicationContext(), MainActivity.class, R.drawable.navigation_tab, R.drawable.baseline_clear_black_24, getString(R.string.myDevice_text_disconnect), getString(R.string.app_name), getString(R.string.bleService_text_connected), getString(R.string.bleService_text_connected))
    );
}

通知渠道:

        NotificationChannel serviceChannel;
        serviceChannel = new NotificationChannel(
                NOTIFICATION_SERVICE_CHANNEL_ID,
                serviceNotificationChannelLabel,
                NotificationManager.IMPORTANCE_HIGH
        );

        serviceChannel.setDescription(serviceNotificationChannelDescription);
        serviceChannel.enableLights(false);
        serviceChannel.setShowBadge(false);
        serviceChannel.enableVibration(false);

        mNotificationManager.createNotificationChannel(serviceChannel);

通知:

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context, NOTIFICATION_SERVICE_CHANNEL_ID)
            .setSmallIcon(icon)
            .setContentTitle(contentTitle) 
            .setContentText(contentText) 
            .setContentIntent(pLaunchIntent)
            .setTicker(tickerText) 
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setOngoing(true)
            .setAutoCancel(false)
            .addAction(icButton1, button1Text, actionPendingIntent); 

    return nBuilder.build();

【问题讨论】:

  • 你尝试添加这个:
    清单中的foregroundServiceType 设置为connectedDevice。我也尝试将其添加到 startForeground 方法中,但并没有影响问题。
  • 它不会出现在 Android 10 上,仅出现在 Android 11 上

标签: android bluetooth-lowenergy foreground-service


【解决方案1】:

对我来说,将活动启动模式从 singleInstance 更改为解决了问题。

改变

        android:launchMode="singleInstance"

        android:launchMode="singleTop"

或完全删除它

很明显,使用 singleInstance 是有正当理由的,这仍然是出乎意料的行为,但目前它是一种有效的解决方法。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签