I created an app which shows Notification when Service.onStartCommand()
method executed and hide notification when Service.onDestroy()
is called. Its works fine for normal calls to startService() and stopService().
Service Code:
@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);
}
Problem: When my service is destroyed by android OS() (In case when onDestroy()
not called) then notification not removed. So how to remove notification when Service is destroyed by android OS itself?
Always call your code BEFORE super.onDestroy();
and not AFTER
@Override
public void onDestroy()
{
nm.cancel(R.string.service_started);
super.onDestroy();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With