从服务中删除持续通知
我有一项在启动时创建通知的服务。
然后 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);
I have a service that create a notification when it is started.
And then ondestroy() i want it to be removed.
I just use .cancel(NOTIFICATION_ID);
It works great when it is a normal notification but when i am using ongoing event it just won't cancel it.
I did read something about that services doesn't stop if android have the resources for it, but how to overcome this?
I use this code to start the service
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);
}
});
I use this in on create
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);
And this in on destroy
noficationManager.cancel(19314);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果可以的话,我会以不同的方式看待“持续”通知。
Service
上有一些方法专门用于添加和删除“正在进行的”通知。Service.startForeground (int,通知)
Service.stopForeground(boolean)
也许您可以尝试从
onDestroy()
调用stopForegroud(boolean)
代码>方法优先...I would look at doing 'ongoing' notifications a bit differently if you can. There are methods that live on
Service
that are dedicated to adding and removing an 'ongoing' notification.Service.startForeground(int, Notification)
Service.stopForeground(boolean)
Perhaps you could just try calling
stopForegroud(boolean)
from youronDestroy()
method first...