android - Android主屏幕小部件:无法更改ImageButton图像资源
问题描述
我正在尝试通过广播消息从服务应用程序更改“AppWidgetProvider”的 ImageButton 图像源。从服务“updateAppWidget”方法被正确调用,但“R.id.widget_play_stop_btn”图像资源没有改变。
private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
if (AppConstants.selectedAudioModel == null) {
Log.e(TAG, "TODO");
return;
}
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.music_player_widget);
remoteViews.setTextViewText(R.id.widget_audio_title, AppConstants.selectedAudioModel.getTitle());
remoteViews.setTextViewText(R.id.widget_audio_artist, AppConstants.selectedAudioModel.getArtist());
int playStopButtonDrawableId = AppConstants.isPlayingMusic ? R.drawable.stop : R.drawable.play;
Log.e(TAG, "updateAppWidget:"+playStopButtonDrawableId+" "+AppConstants.isPlayingMusic);
remoteViews.setImageViewResource(R.id.widget_play_stop_btn, playStopButtonDrawableId);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(appWidgetId, remoteViews);
}
奇怪的信息:当我运行调试并逐行执行代码时,图像资源正确更改。我正在Android 10中进行测试。有人可以帮忙吗?
已编辑:这是调用更新小部件的方式。并且在调用updateMusicPlayerIWidget之前,AppConstants.isPlayingMusic被正确修改
private void updateMusicPlayerWidget() {
Intent intent = new Intent(getApplicationContext(), MusicPlayerWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int ids[] = AppWidgetManager.getInstance(getApplication()).
getAppWidgetIds(new ComponentName(getApplication(), MusicPlayerWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);
}
这是 ImageButton 描述:
<ImageButton
android:id="@+id/widget_play_stop_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:src="@drawable/play"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
这是示例 Logcat:
2020-06-14 22:02:58.336 2347-2347/? E/MusicPlayerWidget: updateAppWidget:2131165330 false
.524 2347-2347/com.example.musicplayer E/MusicPlayerService: prepareMusicPlayer
2020-06-14 22:03:06.559 2347-2347/com.example.musicplayer E/MusicPlayerService: onPrepared
2020-06-14 22:03:06.559 2347-2347/com.example.musicplayer E/MusicPlayerService: startMusicPlayer
2020-06-14 22:03:06.566 2347-2347/com.example.musicplayer E/MusicPlayerWidget: updateAppWidget:2131165331 true
20-06-14 22:03:13.773 2347-2347/com.example.musicplayer E/MusicPlayerService: stopMusicPlayer
2020-06-14 22:03:13.796 2347-2347/com.example.musicplayer E/MusicPlayerWidget: updateAppWidget:2131165330 false
6-14 22:03:19.014 2347-2347/com.example.musicplayer E/MusicPlayerService: prepareMusicPlayer
2020-06-14 22:03:19.034 2347-2347/com.example.musicplayer E/MusicPlayerService: onPrepared
2020-06-14 22:03:19.034 2347-2347/com.example.musicplayer E/MusicPlayerService: startMusicPlayer
2020-06-14 22:03:19.045 2347-2347/com.example.musicplayer E/MusicPlayerWidget: updateAppWidget:2131165331 true
020-06-14 22:03:25.672 2347-2347/com.example.musicplayer E/MusicPlayerService: stopMusicPlayer
2020-06-14 22:03:25.693 2347-2347/com.example.musicplayer E/MusicPlayerWidget: updateAppWidget:2131165330 false
更多信息: 在 Redmi S2 Android 9 中正常工作 在 Galaxy S10e Android 10 中不工作
解决方案
推荐阅读
- c++ - c ++ wcout停止输出,wofstream在一些调用后也停止输出
- recursion - 为什么这个递归函数超过调用堆栈大小?
- javascript - cookie 在 chrome 中不起作用
- php - 是否在被视为验证的输入上使用 HTML 必需属性?
- c - 了解 gcc 的内联(二进制中存在的内联函数汇编代码?)
- google-tag-manager - 可以从谷歌标签管理器中的事件中提取变量
- sublimetext3 - Sublime Text 3 卸载 Materialize 包后出现错误
- c# - 正则表达式过滤代码段
- wordpress - get_terms 过滤器导致未选中类别列表复选框
- javascript - 右键单击和左键单击选项需要靠近单击行