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

我是研究僧i 提交于 2019-12-10 17:42:28

问题


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.

来源:https://stackoverflow.com/questions/53132403/detect-when-app-is-killed-from-recent-apps-list-in-android-o-and-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!