bmurauer
bmurauer

Reputation: 1279

stale preferences in WallpaperService

I'm a beginner in Android development. I'm trying to build a simple live wallpaper app, inspired by https://github.com/cyunrei/Video-Live-Wallpaper. One of the modifications I want to make is to not use the file system to store the preference which video should be used, as it feels hacky and not appropriate. However, I failed to use both the dataStore as well as SharedPreferences for this purpose. I think that I miss something basic, this is what I have set up, and what I expected to happen:

I expected the workflow to happen like this:

  1. In the activity, I set the preference using:
    context.getSharedPreferences(VIDEO_PREFS, 0).edit().putInt(VIDEO_ID, res).commit()
    
    where context is LocalContext.current of the composable function.
  2. after that call, the wallpaper is changed by calling:
    Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER).apply {
      putExtra(
         WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
         ComponentName(context, MyVideoWallpaperService::class.java)
      )
    }.also { context.startActivity(it) }
    
    I must admit that I don't fully understand this part, it is copied from the reference repository mentioned in the text above.
  3. In the wallpaper service, I read the preference using:
    applicationContext.getSharedPreferences(VIDEO_PREFS, 0).getInt(VIDEO_ID, R.raw.example)
    
    to be sure, I currently do this every time the video surface is switched to visible.

However, this is only performed once correctly. After i select a different video in the activity for the second (or any subsequent) times, step 3 is returning the old value. The same behavior appears when i use the dataStore of the context instead of SharedPreferences.

Did i miss something fundamental? Is there a more correct way of achieving what I want?

Thanks for your help

Upvotes: 0

Views: 17

Answers (0)

Related Questions