Nik*_*sen 2 java android android-service android-notifications
我有一个服务,它在启动时创建通知.
然后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);
}
});
Run Code Online (Sandbox Code Playgroud)
我在创建中使用它
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);
Run Code Online (Sandbox Code Playgroud)
这就是毁灭
noficationManager.cancel(19314);
Run Code Online (Sandbox Code Playgroud)
如果可以的话,我会考虑做一些不同的"持续"通知.有些方法Service专门用于添加和删除"正在进行的"通知.
Service.startForeground(int, Notification)
Service.stopForeground(boolean)
也许你可以先尝试stopForegroud(boolean)从你的onDestroy()方法中调用......
| 归档时间: |
|
| 查看次数: |
3346 次 |
| 最近记录: |