how can we set a background task to android so that at given time an image is loaded from URL and set as wallpaper
Asked
Active
Viewed 1,305 times
-2
-
3what exactly is a problem of all those? 1) How to set a background image in Android? 2) How to download file from URL? 3) How to execute code after time? – Vladyslav Matviienko Nov 30 '18 at 11:48
-
1Could you include some code or examples of what you have tried so far ? – DarkMukke Nov 30 '18 at 15:16
1 Answers
3
Use this code to set the wallpaper
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.five);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AND u need to addd this permission in your manfest
<uses-permission android:name="android.permission.SET_WALLPAPER" />
and you need JobDispatcher to runa task periodically.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("DAILY-MAIN-SYNC")
.setRecurring(true) // setRecurring
// don't persist past a device reboot
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(1, (int) TimeUnit.DAYS.toSeconds(1)))
.setExtras(myExtrasBundle)
.build();
dispatcher.schedule(myJob);
this job will run each day
now create MyJobService extends JobService
in in the
@Override
public boolean onStartJob(JobParameters job) { ....
write the code I wrote above.
Hope this will guide you to the right track.

Diaa Saada
- 1,026
- 1
- 12
- 22
-
thank you, Tried it and works some times.But how can we set it to a fixed time everyday or like a 2 hr interval – Frisky Coder Nov 30 '18 at 16:15
-
using this line .setTrigger(Trigger.executionWindow(1, (int) TimeUnit.DAYS.toSeconds(1))) – Diaa Saada Dec 01 '18 at 11:16
-
-
No it should not I use it when there some background tasks that should work even if the user is not using the app – Diaa Saada Dec 02 '18 at 16:59
-
But for me it works until the app stays in the recents, but onece terminated the job is not done.It will initiate agin and works suddenly if the app is agin launched after the given time.Currently using Android 8.0,is there anything extra to add. – Frisky Coder Dec 04 '18 at 13:28
-
Thankyou its working now after turning of androids background optimization on Oxygen OS. – Frisky Coder Dec 05 '18 at 07:43
-