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