我正在开发一个必须始终处于活动状态的应用程序,它将用于专用于应用程序的设备。当应用程序从最近的应用程序窗口中被删除时取消通知
我读过,不让应用程序被操作系统杀死我需要设置一个持续的通知,我也应用了部分唤醒锁。
的问题是该通知不会消失,如果应用程序被杀死了,我把这个在我的活动
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getApplicationContext(), PlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 8000, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Description")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_playlist_play_white_36dp)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon))
.setOngoing(true);
notificationManager.notify(8000, builder.build());
的onCreate
方法上onDestroy
方法我称之为
notificationManager.cancelAll();
super.onDestroy();
通知永远不会被删除,我不得不卸载应用程序被杀死,有没有办法删除它时,应用程序没有加载?
编辑:我针对API 19
不幸的onDestroy方法不这样做一个安全的地方和Android可能不会调用一些这种方法的情况。 可能你必须创建一个服务来处理这种情况,如果有必要的话! –