Detect when app is killed from recent apps list in android O and P
Asked Answered
D

0

3

Use case is I have to send logout request to server when app get killed from recent lists. I use onTaskRemoved to handle that however in Android O and P I get notification bar saying "app is running" that I want to avoid. Here is how I run foreground service:

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        channelId = "my_service_channel_id";
        String channelName = "My Foreground Service";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, channelId)
                        .setContentTitle("")
                        .setAutoCancel(true)
                        .setContentText("");

        startForeground(NOTIFY_ID, builder.build());
        //notificationManager.cancel(NOTIFY_ID); // It doesn't remove notification
        //notificationManager.deleteNotificationChannel(channelId); // it causes crash
    }  

I already tried JobScheduler but onTaskRemoved doesn't get trigger. Any helps would be appreciated.

Dimorphous answered 3/11, 2018 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.