0

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

}
4

1 回答 1

0

我找到了解决方案。我的错误 - 使用带有 setBackground() 的按钮作为图像。在这种情况下 remoteViews.setImageViewResource(R.id.switchLight, R.drawable.blue_bulb) 抛出致命错误。解决方案 - 使用 ImageButton 和 setImageViewResource() 将正常工作。并实际回答我的问题:更改通知中的按钮 - 1. 存储一个 NotificationCompat.Builder 实例,该实例构建了我的通知、notificationID 和 RemoteViews(我的通知内容)。2.使用remoteViews.setImageViewResource()更改按钮src 3.通过notificationManager.notify(notificationID, notificationBuider.build()); 所以,实际上,没有办法改变通知中的按钮。我们应该用小的不同重新创建整个通知。第一段我在 PopupService 中使用静态变量。如您所见,按钮有另一个视图。它工作正常

于 2015-09-01T22:32:04.037 回答