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

504 views Asked by At

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.

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.

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.

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.

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.

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.

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

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

There are 1 answers

0
behelit On BEST ANSWER

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

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.