android - 检测应用程序何时从 android O 和 P 中的最近应用程序列表中被杀死
问题描述
用例是当应用程序从最近的列表中被杀死时,我必须向服务器发送注销请求。我使用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没有被触发。任何帮助将不胜感激。
解决方案
推荐阅读
- api - Sony Soundbar HT-ZF9 API setActiveTerminal 显示错误 12;其他 API 正在工作
- javascript - JavaScript 变量提升行为
- mysql - 从子查询/加入同一个表更新 MySQL 表
- python - 熊猫仅在后一行有值时才填充 na
- python - 将字符串添加到 python 列表的每个元素
- octopus-deploy - 使用 Octopus 中的变量集刷新所有项目的变量
- ajax - 相同的域和协议,但“被 CORS 策略阻止:不存在‘Access-Control-Allow-Origin’”
- firebase - 在阅读列表之间通过 Dialogflow-fulfillment 请求许可
- python - 对字符串变量的实时语音
- android - 内置 apk 导致白屏和标题