0

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?

Denizen
  • 335
  • 5
  • 17
  • See this reference for your question http://stackoverflow.com/questions/3856767/android-keeping-a-background-service-alive-preventing-process-death – Ramkumar Jul 30 '12 at 12:44
  • Thanks for the reference, but I want to kill my service, not keep it from being killed. – Denizen Jul 30 '12 at 12:59

0 Answers0