在 Android 中单击时更新自定义通知中包含的按钮的文本

Update the text of a button contained in a custom notification when clicked in Android

更新

我按照 Pawel 和 Amit K 的建议通过更新通知修改了我的代码。我感谢他们。在 'alarmNotification' 方法中,我使用 id1 创建通知,当单击按钮时,我将变量(字符串)"on" 传递给 'Buttonlistener' [=29] 的 'onReceive' =] 我管理新通知的地方。现在的问题是传递给 onReceive 的字符串变量 "alarm_state" 没有正确地与 If 结构进行比较。你能告诉我为什么吗?

public void alarmNotification(String title, String message)
{
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    notify.contentView=contentView;

    Intent button_intent=new Intent(context,ButtonListener.class);
    button_intent.putExtra("alarm_state","on");
    PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
    contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);

    nm.notify(1,notify);
}

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition=="on")  //here it's always false!!! Why??
        {                
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
            contentView.setTextViewText(R.id.subtitle,"Cam enabled");
        }
        if (condition=="off")
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }
}

在我的应用程序中,我有一个带有按钮的个性化通知,按下该按钮时必须将其文本从 "Active" 更改为 "Disabled"。通知由服务启动,点击事件触发服务中包含的 class "ButtonListener" 应该更改按钮的文本。问题是按钮的文字没有改变!!!

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        Toast.makeText(context, "Button clicked", Toast.LENGTH_SHORT).show();  // this ok
        try {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_notification, null);                
            Button button = (Button) layout.findViewById(R.id.switch1);
            button.setText("Disabled");   // this not update!!!
        }
        catch (Exception ec)
        {

        }

    }
}

你膨胀了一些新的布局,当 onReceive 完成后它会立即被垃圾收集。

遗憾的是,您必须创建具有更新视图的通知并通知 NotificationManager 替换旧视图。

更新

 public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition.compareToIgnoreCase("on")==0)  //here it's always false!!! Why??
        {                
             contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
             contentView.setTextViewText(R.id.subtitle,"Cam enabled");
             Intent button_intent=new Intent(context,ButtonListener.class);
             button_intent.putExtra("alarm_state","off");
             PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,FLAG_UPDATE_CURRENT);
             contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
        }
        if (condition.compareToIgnoreCase("off")==0)
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
            Intent button_intent=new Intent(context,ButtonListener.class);
            button_intent.putExtra("alarm_state","on");
            PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
            contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }

您无法在创建通知后更改操作按钮的标题。但是你可以做什么,你可以设置一个背景 drawable,它会根据状态而不同。像这样 -

假设您有 my_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/button_on_image" android:state_pressed="true"/>
  <item android:drawable="@drawable/button_on_image" android:state_focused="true"/>
  <item android:drawable="@drawable/button_off_image"/>
</selector>

然后-

addAction(R.drawable.my_button, <title>, <intent>);

或者您可以按照@Pawel 所说的进行操作,但那是创建另一个新通知来替换旧通知。

我按照 Pawel 和 Amit K 的建议通过更新通知修改了我的代码。我感谢他们。在 'alarmNotification' 方法中,我使用 id1 创建通知,当单击按钮时,我将变量(字符串)"on" 传递给 'Buttonlistener' [=18] 的 'onReceive' =] 我管理新通知的地方。现在的问题是传递给 onReceive 的字符串变量 "alarm_state" 没有正确地与 If 结构进行比较。你能告诉我为什么吗?

public void alarmNotification(String title, String message)
{
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    notify.contentView=contentView;

    Intent button_intent=new Intent(context,ButtonListener.class);
    button_intent.putExtra("alarm_state","on");
    PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
    contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);

    nm.notify(1,notify);
}

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition=="on")  //here it's always false!!! Why??
        {                
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
            contentView.setTextViewText(R.id.subtitle,"Cam enabled");
        }
        if (condition=="off")
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }
}