Search code examples
androidandroid-studioandroid-jobschedulerandroid-wallpaper

Schedule auto wallpaper changing in background


how can we set a background task to android so that at given time an image is loaded from URL and set as wallpaper


Solution

  • 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.