0

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

@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?

4

0 回答 0