繁体   English   中英

Android 11 - 按下主页按钮时触发 foregroundService 的 onTaskRemoved

[英]Android 11 - foregroundService's onTaskRemoved is trigged when home button pressed

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

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

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

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

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

根据我的理解,只有当用户通过在任务切换器中滑动应用程序来终止应用程序时,才会触发此方法。

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

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

我试过在这篇文章中关注 dumpsys 但它似乎有所不同,我找不到提到的任何参考资料。

通知本身如下所示:

$ 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();

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

改变

        android:launchMode="singleInstance"

        android:launchMode="singleTop"

或完全删除它

显然,使用 singleInstance 是有正当理由的,这仍然是意外行为,但现在它是一种有效的解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM