Class of service:
public class KillNotificationsService extends Service {
public static int NOTIFICATION_ID = 1;
private NotificationManager mNM;
private final IBinder mBinder = new KillBinder(this);
public class KillBinder extends Binder {
public final Service service;
public KillBinder(Service service) {
this.service = service;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public void onCreate() { }
@Override
public void onTaskRemoved(Intent rootIntent){
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.cancel(NOTIFICATION_ID);
}
}
Method within Activity
private void serviceNotification(){
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,IBinder binder) {
((KillNotificationsService.KillBinder) binder).service.startService(new Intent
(MontarTimesActivity.this, KillNotificationsService.class));
NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.notify(KillNotificationsService.NOTIFICATION_ID, notification().getNotification());
}
public void onServiceDisconnected(ComponentName className) {
}
};
bindService(new Intent(MontarTimesActivity.this, KillNotificationsService.class), mConnection, Context.BIND_AUTO_CREATE);
}