服务没有被PendingIntent杀死

时间:2012-07-30 12:42:13

标签: android notifications persistence

我有一个发布通知的服务,而我想要做的就是在点击通知时终止该服务。这是我的代码:

@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();
    }

}

当我点击通知时,状态栏被解除,但通知仍然存在。为什么StopSelf没有查杀服务并调用onDestroy?

0 个答案:

没有答案