3

開始時に通知を作成するサービスがあります。

そして ondestroy() を削除したいです。

.cancel(NOTIFICATION_ID); を使用するだけです。

通常の通知の場合はうまく機能しますが、進行中のイベントを使用している場合はキャンセルされません。

Androidにリソースがある場合、サービスが停止しないことについて何か読んだことがありますが、これを克服するにはどうすればよいですか?

このコードを使用してサービスを開始します

    final Intent bg_service = new Intent(BackgroundService.class.getName());

    btn_start = (Button)findViewById(R.id.btn_start);
    btn_stop = (Button)findViewById(R.id.btn_stop);

    btn_start.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(bg_service);
        }
    });

    btn_stop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(bg_service);
        }
    });

これを作成時に使用します

            notificationManager = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

    Intent intent = new Intent(this, mainapp.class);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "...",
            "...", activity);
    notificationManager.notify(19314, notification);

そして、これを破壊します

            noficationManager.cancel(19314);
4

1 に答える 1

9

可能であれば、「進行中」の通知を少し違った方法で行うことを検討します。Service「進行中」の通知の追加と削除専用のメソッドがあります。

Service.startForeground(int, Notification)

Service.stopForeground(boolean)

stopForegroud(boolean)おそらく、最初にメソッドから呼び出してみることができますonDestroy()...

于 2012-02-15T22:28:00.537 に答える