【问题标题】:How change action icons dynamically in android wear如何在 android wear 中动态更改动作图标
【发布时间】:2014-07-31 01:41:10
【问题描述】:

我创建了一个支持我的手持应用程序的 android wear 应用程序。 显示通知和相关操作的 android wear 应用表现最佳。

我创建了 android wear 通知并添加了暂停、恢复和停止两个动作

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

我的代码是

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();

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

提前谢谢大家... 我的屏幕看起来像

【问题讨论】:

    标签: android android-pendingintent wear-os


    【解决方案1】:

    解决方案

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



    另外:我建议你在这里实现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>
    

    【讨论】:

    • 那么我需要从哪里更新通知?我有一个针对 pausePendingIntent 的活动,来自 pauseActivity,我可以更新吗?
    • 基本上你可以从任何地方更新通知(你只需要一个context)。顺便提一句。你为什么使用活动作为你的行动pendingIntents?单击该操作后,您的设备需要显示任何内容吗?如果没有(如果只是恢复/暂停/停止音乐),您应该使用BroadcastReceiver(而不是Activity)。它可以让您在不显示任何 UI 的情况下处理操作。
    • 是的,没错。单击操作后我不想显示任何内容,我该如何为此目的实现广播接收器,如果您不介意可以帮我举个例子吗?
    • 没问题。我还添加了这些动作名称的声明作为最终的静态字符串字段。这样您就不需要在每次使用时都重新键入它们。这将帮助您在以后的代码更改中减少出错的机会:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多