1

I am creating application where there is an option of checkbox allowing user to change wallpaper automatically after 30 seconds. I am using JobScheduler and have passed arraylist of images by serializing them to JsonArray and then to String and passed it using PersistanceBundle:

JsonArray result = (JsonArray) new Gson().toJsonTree(wallpaperModelArrayList,
                        new TypeToken<List<WallpaperModel>>() {
                        }.getType());
PersistableBundle persistableBundle=new PersistableBundle();
persistableBundle.putString("wallpaper",result.toString());
mJobScheduler = (JobScheduler)
                        getSystemService(Context.JOB_SCHEDULER_SERVICE);
                JobInfo.Builder builder = new JobInfo.Builder(1,
                        new ComponentName(getPackageName(),
                                JobSchedulerService.class.getName()));
                builder.setExtras(persistableBundle);
                builder.setPeriodic(30000);

And my JobService class:

public class JobSchedulerService extends JobService {

private static final String TAG = "JobSchedulerService";
private String images;

@Override
public boolean onStartJob(JobParameters params) {
    Log.i(TAG, "onStartJob:");
    images = params.getExtras().getString("wallpaper");
    changeWallpaper(params);
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    //Log.i(TAG, "onStopJob:");
    return true;
}

private void changeWallpaper(JobParameters params) {

    Gson gson = new Gson();
    Type listType = new TypeToken<List<WallpaperModel>>(){}.getType();
    List<WallpaperModel> list = gson.fromJson(images, listType);

    Glide.with(this)
            .load(list.get(0).getUrlImage())
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super
                        Bitmap> glideAnimation) {
                    try {
                        WallpaperManager.getInstance(getApplicationContext()).setBitmap(resource);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
    jobFinished(params, false);
}

}

I don't know how to set wallpaper(images one by one) after every 30 secs out of the list I have in JobService?Can Anyone tell me how setperiodic function works(what happens after 30secs)? This approach can be wrong. Can anyone guide me on how to do this? Thanks in advance.

RahulS
  • 11
  • 6

0 Answers0