Android 11-按下主屏幕按钮时会触发前台服务的onTaskRemoved [英] Android 11 - foregroundService's onTaskRemoved is trigged when home button pressed

查看:100
本文介绍了Android 11-按下主屏幕按钮时会触发前台服务的onTaskRemoved的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am seeing some strange behaviour on a Pixel 4XL (Android 11). My foreground service's onTaskRemoved is being called unexpectedly. 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.

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

My app uses an ongoing BLE connection to communicate with another non-android device. A foreground service is used to ensure the program remains active to receive BLE communications from the device.

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

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.

几乎可以确保在我的应用程序中打开其他活动(即MainActivity以外的活动).

Opening a different activity in my app (i.e. an activity other than MainActivity) is almost guaranteed to make this occur.

从通知栏中打开设置,然后向内滑动仍然会触发此操作,因此,并不是因为我在设置应用程序处于焦点时向上滑动而意外杀死了该应用程序.

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.

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

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.

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

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. I am not calling onTaskRemoved myself anywhere in code.

我已经尝试遵循转储在这篇文章中,但似乎会有所不同,我找不到提到的任何参考文献.

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.

通知本身如下列出:

$ 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方法(已删除一些不相关的内容):

BLEService onCreate method (some irrelevant removed):

@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 更改活动启动模式可以解决此问题.

For me, changing the activity launch mode away from singleInstance resolved the problem.

更改

        android:launchMode="singleInstance"

        android:launchMode="singleTop"

或将其完全删除

显然,拥有singleInstance是有充分理由的,这仍然是意料之外的行为,但是现在这是一个有效的解决方法.

Obviously there are valid reasons for having singleInstance and this is still unexpected behaviour but for now it's a valid workaround.

这篇关于Android 11-按下主屏幕按钮时会触发前台服务的onTaskRemoved的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆