1

I am trying to change ImageButton image source of a "AppWidgetProvider" from a service app by broadcasting a message. From service "updateAppWidget" method is called properly but "R.id.widget_play_stop_btn" image resource not changing.

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

Strange information: When I run debug and execute code line by line, image resources changed properly.I am testing in Android 10. Can anyone please help?

Edited: This is how update widget is called. And before calling updateMusicPlayerIWidget, AppConstants.isPlayingMusic is modified properly

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

This is the ImageButton description:

<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"/>

This is sample 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

More Information: Works properly in Redmi S2 Android 9 Does not work in Galaxy S10e Android 10

Gangcil
  • 440
  • 3
  • 9
  • Since the drawable is chosen based on `AppConstants.isPlayingMusic`: when and how is this value updated, and when and how do you trigger `updateAppWidget()`? – Bö macht Blau Jun 14 '20 at 15:35
  • I modified the question description and tried to answer your query. Can you kindly check? – Gangcil Jun 14 '20 at 15:45
  • You are already writing the drawable resource id value to Logcat - could you also add the value of `AppConstants.isPlayingMusic`? – Bö macht Blau Jun 14 '20 at 16:01
  • Hello Blau, I have added a log as per your instruction. Image did not change for this case also except for the first time app is loaded – Gangcil Jun 14 '20 at 16:07
  • There is [an older post](https://stackoverflow.com/questions/7901138/app-widget-setimageviewuri-does-not-update-the-image) about updating an ImageView in an app widget. Although the scenario is slightly different, the post may be interesting for you because the accepted answer does not seem to work any more for newer API levels. One comparatively recent answer used a workaround involving bitmaps. – Bö macht Blau Jun 14 '20 at 16:21
  • 1
    Hello Blau, I have added more information. **More Information:** Works properly in Redmi S2 Android 9 Does not work in Galaxy S10e Android 10 – Gangcil Jun 14 '20 at 16:23
  • How about an emulator running Android 10? (can it be mainly a Samsung issue?) – Bö macht Blau Jun 14 '20 at 16:28
  • I did not check in the emulator. After you mention I am setting up the emulator. It will take a few times I guess. Kindly wait – Gangcil Jun 14 '20 at 16:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215953/discussion-between-gangcil-and-bo-macht-blau). – Gangcil Jun 14 '20 at 16:53

0 Answers0