在android O和P中检测何时从最近的应用列表中删除应用
用例是当应用程序从最近的列表中被杀死时,我必须向服务器发送注销请求。我使用onTaskRemoved来处理这个问题,但是在安卓O和P中,我会收到一个通知栏,上面写着“应用正在运行”,这是我想要避免的。以下是我如何运行前台服务:在android O和P中检测何时从最近的应用列表中删除应用,android,notifications,foreground,android-9.0-pie,android-recents,Android,Notifications,Foreground,Android 9.0 Pie,Android Recents,用例是当应用程序从最近的列表中被杀死时,我必须向服务器发送注销请求。我使用onTaskRemoved来处理这个问题,但是在安卓O和P中,我会收到一个通知栏,上面写着“应用正在运行”,这是我想要避免的。以下是我如何运行前台服务: if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationManager notificationManager = (Not
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没有触发。任何帮助都将不胜感激