Mar*_*ren 1 notifications android remoteview android-pendingintent
我有一个使用 remoteView 的通知。在通知上我有 3 个按钮,每个按钮都有自己的待处理意图。出于某种原因,只有我使用 setOnClickPendingIntent 添加的最后一个 pendingIntent 有效。
这里有一些
Intent intentPause = new Intent(context, BluetoothConnectionService.class);
intentPause.putExtra("pause", device.device.getAddress());
Intent intentIncrease = new Intent(context, BluetoothConnectionService.class);
intentIncrease.putExtra("inc", device.device.getAddress());
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 0, intentIncrease, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
expandedView.setOnClickPendingIntent(R.id.noti_pause_heat_button, pendingIntentPause);
expandedView.setOnClickPendingIntent(R.id.noti_inc_heat_button, pendingIntentIncrease);
Notification notification = new Notification.Builder(context)
.setContentTitle(this.device.getName())
.setSmallIcon(R.drawable.inu_small)
.setContentText("" + this.notificationId)
.setContent(expandedView)
.build();
Run Code Online (Sandbox Code Playgroud)
为每个使用不同的requestCode(的第二个参数getService)PendingIndent。例如
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 1, intentIncrease, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)