我有一项在启动时创建通知的服务。
然后 ondestroy() 我希望将其删除。
我只是使用 .cancel(NOTIFICATION_ID);
当它是正常通知时它效果很好,但当我使用正在进行的事件时它不会取消它。
我确实读到过一些关于如果 android 有资源的话服务就不会停止的内容,但是如何克服这个问题呢?
我使用这段代码来启动服务
final Intent bg_service = new Intent(BackgroundService.class.getName());
btn_start = (Button)findViewById(R.id.btn_start);
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(bg_service);
}
});
btn_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(bg_service);
}
});
我在创建时使用它
notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A new notification", System.currentTimeMillis());
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Intent intent = new Intent(this, mainapp.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "...",
"...", activity);
notificationManager.notify(19314, notification);
这即将被摧毁
noficationManager.cancel(19314);