如何在android中动态更改动作图标

时间:2014-07-28 12:23:39

标签: android android-pendingintent wear-os

我创建了一个支持我的掌上电脑应用程序的android服装应用程序。 Android服装应用程序显示通知和相关操作最佳执行。

我已经创建了android磨损通知,并为暂停,恢复和停止添加了两个动作

我需要动态地通过恢复操作替换暂停操作,这意味着如果用户按下暂停操作需要更改以恢复操作。

我的代码是

Intent pauseIntent = new Intent(getApplicationContext(), PauseActivity.class);
         PendingIntent pausePendingIntent = PendingIntent.getActivity(getApplicationContext(),
                 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

         Intent stopIntent = new Intent(getApplicationContext(), StopActivity.class);
         PendingIntent stopPendingIntent = PendingIntent.getActivity(getApplicationContext(),
                 0, stopIntent,  PendingIntent.FLAG_UPDATE_CURRENT);


         Notification notification =
                    new NotificationCompat.Builder(getApplicationContext())
                            .setSmallIcon(R.drawable.icon)
                            .setContentTitle("Swipe up to view")
                            .setDeleteIntent(deletePendingIntent)
                            .extend(new WearableExtender()
                                    .setDisplayIntent(displayPendingIntent))
                            .addAction(R.drawable.pause_btn, "Pause", pausePendingIntent)
                            .addAction(R.drawable.stop_btn, "Stop", stopPendingIntent)
                                    .build();

我的要求是当用户点击暂停按钮时,我需要通过恢复图标更改它。

感谢所有人...... 我的屏幕看起来像

enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

解决方案

您需要在用户点击该操作时发布新通知才能刷新 旧通知将更新,因此在发布新通知时,将“暂停”图标更改为“恢复”图标+对标签执行相同操作。



另外:我建议您在这里实施BroadcastReceiver

定义你的意图:

Intent pauseIntent = new Intent(MediaPlayerActionsReceiver.ACTION_PAUSE, null, context, MediaPlayerActionsReceiver.class);
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(context, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent resumeIntent = new Intent(MediaPlayerActionsReceiver.ACTION_RESUME, null, context, MediaPlayerActionsReceiver.class);
PendingIntent resumePendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent stopIntent = new Intent(MediaPlayerActionsReceiver.ACTION_STOP, null, context, MediaPlayerActionsReceiver.class);
PendingIntent stopPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);

它们都将被传递到同一个组件(MediaPlayerActionsReceiver),但声明了不同的操作。


然后创建MediaPlayerActionsReceiver类:

public class MediaPlayerActionsReceiver extends BroadcastReceiver {

    public final static String ACTION_PAUSE = "com.example.package.receiver.action_pause";
    public final static String ACTION_RESUME = "com.example.package.receiver.action_resume";
    public final static String ACTION_STOP = "com.example.package.receiver.action_stop";
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent==null)
            return;

        final String action = intent.getAction(); 
        if(ACTION_PAUSE.equals(action)) {
            // handle pause action and refresh notification
        } else if(ACTION_RESUME.equals(action)) {
            // handle resume action and refresh notification
        } else if(ACTION_STOP.equals(action)) {
            // handle stop action and refresh notification
        }
    }
}

最后一步是在AndroidManifest

中声明您的接收者
<receiver android:name="com.example.package.receiver.MediaPlayerActionsReceiver" android:enabled="true" android:exported="false">
    <intent-filter>
        <action android:name="com.example.package.receiver.action_pause" />
        <action android:name="com.example.package.receiver.action_resume" />
        <action android:name="com.example.package.receiver.action_stop" />
    </intent-filter>
</receiver>