2

BroadcastReceiverが呼び出されたときに「Dim」と「Full」の2つのオプションで通知を表示するアプリを構築しています。このボタンのそれぞれがアクションをブロードキャストします。

これまでのところ、すべてが可能ですよね?

問題は、通知に表示されるボタンがタップに反応しないが、通知全体が反応することです (ボタンの代わりにアイコンまたはテキストをクリックした場合)。

私は通知を作成するためにこの機能を持っています:

private Notification buildReleaseNotification(NotificationManager nManager, Context context) {

    Notification.Builder builder = new Builder(context);
    builder.addAction(R.drawable.rate_star_big_half_holo_dark, "Dim", buildPendingIntent(context, DIM));
    builder.addAction(R.drawable.rate_star_big_on_holo_dark, "Full", buildPendingIntent(context, FULL));

    builder.setContentTitle("Car notification");
    builder.setContentText("Freed");
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setOngoing(true);

    Notification notification = builder.build();
    return notification;
}

ブロードキャストを受信すると呼び出されます。

    public void onReceive(Context context, Intent intent) {

    NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification noty = null;
    noty = buildReleaseNotification(context);
    startService(context, RELEASE);

    if (noty != null) {
        nManager.notify(ChangeLockService.GLOBAL_TAG, ID, noty);
    }

}

- - - 編集

次の関数が null を返すことに気付きました...では、ブロードキャストを実行する保留中のインテントを作成するにはどうすればよいでしょうか?

private PendingIntent buildPendingIntent(Context context, String action) {

        Intent i = new Intent(action);
        PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, Intent.FLAG_RECEIVER_REPLACE_PENDING);
        return pi;
    }
4

1 に答える 1

1

を通じてインテントを作成する場合

PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, Intent.FLAG_RECEIVER_REPLACE_PENDING);

null を返すようにしました。ドキュメントによると、フラグIntent.FLAG_RECEIVER_REPLACE_PENDINGは、そのボタンに既に存在する保留中のインテントを置き換えることを理解していました。フラグを送信しない場合、すべて正常に動作します。

PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, 0);
于 2013-01-21T12:27:25.657 に答える