Issue
I initialized a WallpaperService to use it with the WallpaperManager.
My Code looks like this right now:
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, WallpaperService.class));
startActivity(intent);
My question is, how can I add a color into the Intent and use it in the WallpaperService? The first idea i got was to add
intent.putExtra("backGroundColor", mDefaultColor);
But this value isn't reachable in the WallpaperService.
The current Service looks like this:
public class GameOfLifeWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new GameOfLifeWallpaperEngine();
}
private class GameOfLifeWallpaperEngine extends Engine {
int backgroundColor;
...
}
}
Solution
In order to get the intent and it's extras/bundle, you need to Override the onStartCommand
method inside the service.
There you will have access to the intent and should use a bundle to get the extras.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Bundle bundle = intent.getExtras();
int color = (int) bundle.get("backGroundColor");
return START_REDELIVER_INTENT;
Notice that the method should return one of the START_STICKY_COMPATIBILITY, START_STICKY, START_NOT_STICKY, or START_REDELIVER_INTENT values.
Sometimes the intent data can get lost due to an error. If you want to redeliver the same intent data, use START_REDELIVER_INTEN.
You can read more about it on the android.developers.com website.
Answered By - Filip Petrovski