如何更改通知中的按钮?
How to change button in notification?
我正在制作前台服务通知以控制我设备上的功能。现在我收到了带有手电筒按钮的通知。此外,我可以使用 PendingIntent 处理此按钮。我想在按下时用另一种颜色制作按钮。我怎样才能做到这一点?可能,我错过了一些明显的东西。也许,我可以通过 onReceive 方法中的上下文来完成,但我仍然不知道该怎么做。请帮帮我。
//It's method in Service that I started from MainActivity
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification;
//Create notification builder
NotificationCompat.Builder notificationBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this);
notification = notificationBuilder.setSmallIcon(R.drawable.white_bulb)
.setContentTitle("My Title")
.setContentText("My Text")
.build();
//create a remoteViews to this notification
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notificationlayout);
remoteViews.setImageViewResource(R.id.image, R.drawable.white_bulb);
remoteViews.setTextViewText(R.id.title, "Popup Notification");
remoteViews.setTextViewText(R.id.text, "");
//create a pendingIntent for button in my notification
Intent switchIntent = new Intent(this, SwitchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.switchLight, pendingSwitchIntent);
notification.contentView = remoteViews;
//actually run the notification
startForeground(notificationID, notification);
return super.onStartCommand(intent, flags, startId);
}
//It's method in SwitchButtonListener.class that run when I push the button
public void onReceive(Context context, Intent intent) {
if (!isFlash){
holdCamera();
runFlashLight();
isFlash = true;
}else {
stopFlashLight();
unholdCamera();
isFlash = false;
}
}
我找到了解决方案。我的错误 - 将 Button 与 setBackground() 一起用于图像。在这种情况下 remoteViews.setImageViewResource(R.id.switchLight, R.drawable.blue_bulb) 抛出致命错误。解决方案 - 使用 ImageButton 和 setImageViewResource() 将正常工作。实际上回答我的问题:
用于更改通知中的按钮 -
1. 存储构建我的通知、notificationID 和 RemoteViews(我的通知内容)的 NotificationCompat.Builder 实例。
2.Change 按钮 src 与 remoteViews.setImageViewResource()
3.Via notificationManager.notify(notificationID, notificationBuider.build());
所以,实际上,没有办法改变通知中的按钮。我们应该用小的不同重新创建整个通知。
第一段我在 PopupService 中使用静态变量。如您所见,按钮具有另一种视图。
我正在制作前台服务通知以控制我设备上的功能。现在我收到了带有手电筒按钮的通知。此外,我可以使用 PendingIntent 处理此按钮。我想在按下时用另一种颜色制作按钮。我怎样才能做到这一点?可能,我错过了一些明显的东西。也许,我可以通过 onReceive 方法中的上下文来完成,但我仍然不知道该怎么做。请帮帮我。
//It's method in Service that I started from MainActivity
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification;
//Create notification builder
NotificationCompat.Builder notificationBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this);
notification = notificationBuilder.setSmallIcon(R.drawable.white_bulb)
.setContentTitle("My Title")
.setContentText("My Text")
.build();
//create a remoteViews to this notification
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notificationlayout);
remoteViews.setImageViewResource(R.id.image, R.drawable.white_bulb);
remoteViews.setTextViewText(R.id.title, "Popup Notification");
remoteViews.setTextViewText(R.id.text, "");
//create a pendingIntent for button in my notification
Intent switchIntent = new Intent(this, SwitchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.switchLight, pendingSwitchIntent);
notification.contentView = remoteViews;
//actually run the notification
startForeground(notificationID, notification);
return super.onStartCommand(intent, flags, startId);
}
//It's method in SwitchButtonListener.class that run when I push the button
public void onReceive(Context context, Intent intent) {
if (!isFlash){
holdCamera();
runFlashLight();
isFlash = true;
}else {
stopFlashLight();
unholdCamera();
isFlash = false;
}
}
我找到了解决方案。我的错误 - 将 Button 与 setBackground() 一起用于图像。在这种情况下 remoteViews.setImageViewResource(R.id.switchLight, R.drawable.blue_bulb) 抛出致命错误。解决方案 - 使用 ImageButton 和 setImageViewResource() 将正常工作。实际上回答我的问题:
用于更改通知中的按钮 -
1. 存储构建我的通知、notificationID 和 RemoteViews(我的通知内容)的 NotificationCompat.Builder 实例。
2.Change 按钮 src 与 remoteViews.setImageViewResource()
3.Via notificationManager.notify(notificationID, notificationBuider.build());
所以,实际上,没有办法改变通知中的按钮。我们应该用小的不同重新创建整个通知。
第一段我在 PopupService 中使用静态变量。如您所见,按钮具有另一种视图。