首页 > 解决方案 > 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 中不工作

标签: androidandroid-widgetandroid-imagebutton

解决方案


推荐阅读