我创建了一个应用,在Service.onStartCommand()
方法执行时显示通知,并在调用Service.onDestroy()
时隐藏通知。对于普通的startService()和stopService()调用,它可以正常工作。
服务代码:
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//Some code here
showNotification();
return START_NOT_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
nm.cancel(R.string.service_started);
}
问题: 当我的服务被安卓操作系统销毁时(在onDestroy()
未被调用的情况下),通知没有被移除。那么当服务被安卓操作系统销毁时,如何移除通知?