简体   繁体   English

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

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

I am seeing some strange behaviour on a Pixel 4XL (Android 11).我在Pixel 4XL (Android 11)上看到了一些奇怪的行为 My foreground service's onTaskRemoved is being called unexpectedly.我的前台服务的 onTaskRemoved 被意外调用。 This doesn't occur on any other devices but I don't have any other Android 11 devices and the emulator can't do BLE.这不会在任何其他设备上发生,但我没有任何其他 Android 11 设备并且模拟器无法执行 BLE。

My app uses an ongoing BLE connection to communicate with another non-android device.我的应用程序使用正在进行的 BLE 连接与另一个非安卓设备进行通信。 A foreground service is used to ensure the program remains active to receive BLE communications from the device.前台服务用于确保程序保持活动状态以接收来自设备的 BLE 通信。

It doesn't always happen but in most cases (75% of the time) after pressing home (swiping up from the bottom of the screen on a Pixel 4XL) this will cause the foreground service's onTaskRemoved to be called.它并不总是发生,但在大多数情况下(75% 的时间),在按下主页(从 Pixel 4XL 的屏幕底部向上滑动)后,这将导致前台服务的 onTaskRemoved 被调用。

Opening a different activity in my app (ie an activity other than MainActivity) is almost guaranteed to make this occur.在我的应用程序中打开不同的活动(即 MainActivity 以外的活动)几乎肯定会发生这种情况。

Opening settings from the notification bar then swiping home still triggers this to occur, so it can't be that I'm accidentally killing the app since I'm swiping up when the settings app is in focus.从通知栏打开设置然后向家滑动仍然会触发这种情况发生,所以不可能是我不小心杀死了应用程序,因为当设置应用程序处于焦点时我正在向上滑动。

From my understanding, this method is only supposed to be triggered when the user terminates the app by swiping it in the task switcher.根据我的理解,只有当用户通过在任务切换器中滑动应用程序来终止应用程序时,才会触发此方法。

If I go back to the task switcher, after the service was killed, my app is still available and showing the last activity opened and it's state.如果我回到任务切换器,在服务被终止后,我的应用程序仍然可用并显示上次打开的活动及其状态。 Switching to it resumes to the correct place, so the app itself must not have been terminated.切换到它会恢复到正确的位置,因此应用程序本身一定没有被终止。 The phone is plugged in an charging so it shouldn't be and doze/sleeping functions.手机已插入充电,因此它不应该和打瞌睡/睡眠功能。 This can occur within 60 seconds of launching the app, it's not a 30 minute thing.这可能会在启动应用程序后 60 秒内发生,而不是 30 分钟。

What on earth could be going on here?这到底是怎么回事? The only thing I can think of is that it's not considered a foreground service for some reason.我唯一能想到的是,由于某种原因,它不被视为前台服务。 There are no errors in logcat at or around the time of the onTaskRemoved.在 onTaskRemoved 时或前后,logcat 中没有错误。 I am not calling onTaskRemoved myself anywhere in code.我没有在代码中的任何地方调用 onTaskRemoved 自己。

I've tried following the dumpsys in this post but it seems to be different and I couldn't find any of the references mentioned.我试过在这篇文章中关注 dumpsys 但它似乎有所不同,我找不到提到的任何参考资料。

The notification itself is listed like this:通知本身如下所示:

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

Yes, I have the foreground permission set in manifest:是的,我在清单中设置了前台权限:

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

BLEService onCreate method (some irrelevant removed): 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))
    );
}

Notification Channel:通知渠道:

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

Notification:通知:

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

For me, changing the activity launch mode away from singleInstance resolved the problem.对我来说,将活动启动模式从singleInstance更改为解决了这个问题。

Change改变

        android:launchMode="singleInstance"

to

        android:launchMode="singleTop"

or remove it altogether或完全删除它

Obviously there are valid reasons for having singleInstance and this is still unexpected behaviour but for now it's a valid workaround.显然,使用 singleInstance 是有正当理由的,这仍然是意外行为,但现在它是一种有效的解决方法。

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

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