从android O和P中的最新应用列表中检测何时杀死应用

时间:2018-11-03 14:45:54

标签: android notifications foreground android-9.0-pie android-recents

用例是当应用程序从最近的列表中被杀死时,我必须向服务器发送注销请求。我使用 onTaskRemoved 来处理该问题,但是在Android O P 中,我收到通知栏,提示我要避免“应用程序正在运行”。这是我运行前台服务的方式:

   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
    }  

我已经尝试过 JobScheduler ,但是 onTaskRemoved 不会触发。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题