gpt4 book ai didi

android - 如何在 Android Wear 中动态更改 Action 图标

转载 作者:行者123 更新时间:2023-11-29 21:04:30 25 4
gpt4 key购买 nike

我已经创建了一个支持我的手持应用程序的 android wear 应用程序。显示通知和相关操作的 Android Wear 应用。

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

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

我的代码是

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

最佳答案

解决方案

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



另外:我建议你在这里实现一个BroadcastReceiver

像这样定义你的 Intent :

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>

关于android - 如何在 Android Wear 中动态更改 Action 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24995197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com