我正在发出前台服务通知以控制设备上的功能。现在我收到了带有 FlashLight 按钮的通知。此外,我可以使用 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;
}
}