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;
}