18

通知がいつクリアされるかを検出しようとしています。私の質問は、私がやろうとしていることを概説するこの回答を直接参照しています。これが私がアクションを実装する方法です:

// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)

これは CleanUpIntent クラスです。

class CleanUpIntent extends IntentService {
    public CleanUpIntent() {
        super("CleanUpIntent");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // clean up code
    }
}

その後、通常のように通知を起動するだけですが、テストしてみると ([すべての通知をクリア] を押します)、何も起こりません。IntentService の開始時に LogCat に何かを出力するコード行を挿入しましたが、何も実行されませんでした。これは私が Notification.deleteIntent を使用する方法ですか?

4

4 に答える 4

50

ユーザーが通知をクリアするたびに呼び出されるサンプル コードです。お役に立てば幸いです。

 ....
 notificationBuilder.setDeleteIntent(getDeleteIntent());
 ....


protected PendingIntent getDeleteIntent()
 {
    Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

NotificationBroadcastReceiver.java

@Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if(action.equals("notification_cancelled"))
        {
            // your code
        }
    }

AndroidManifest.xml

 <receiver android:name=".NotificationBroadcastReceiver">
                <intent-filter>
                    <action android:name="notification_cancelled"/>
                </intent-filter>
            </receiver>
于 2013-02-06T07:21:51.290 に答える
4

あなたがする必要があるのは、BroadcastReceiver(おそらく AndroidManifest.xml で、または代わりに a で使用registerReceiverして) を登録し、そのレシーバーによってキャッチされる にService設定deleteIntentすることです。Intent

于 2011-09-19T02:19:23.200 に答える
0

getServiceの代わりにgetBroadcastメソッドを使用し、特定のアクションのレシーバーを登録する必要があります。

于 2012-07-19T15:16:51.123 に答える