首页 > 解决方案 > 检测应用程序何时从 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
    }  

我已经尝试过JobScheduleronTaskRemoved没有被触发。任何帮助将不胜感激。

标签: androidnotificationsforegroundandroid-9.0-pieandroid-recents

解决方案


推荐阅读