I have a service that posts a notification, and what I am trying to do is kill that service when the notification is clicked. Here is my code:
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.ic_launcher,
getResources().getString(R.string.notify_body),
System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(),
getResources().getString(R.string.notify_title), getResources()
.getString(R.string.notify_body), PendingIntent
.getActivity(getApplicationContext(), 0, new Intent(
this, StopSelf.class), Intent.FLAG_ACTIVITY_NEW_TASK));
n.defaults = Notification.DEFAULT_ALL;
nm.notify(ID, n);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
nm.cancel(ID);
}
public class StopSelf extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
stopService(new Intent(this, myService.class));
this.finish();
}
}
When I click the notification the statusbar is dismissed, but the notification is still there. Why isn't StopSelf killing the service and calling onDestroy?