Reputation: 9
I am developing an application that shows the different photos from server and user can set select photos as wallpaper of its device i used given code to set wallpaper it working but i want to wallpaper set automatically every day. I used this code.
Java
private void setAsWallpaper()
{
Picasso.get().load(imageUrl).into(new Target()
{
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
{
progressBar.bringToFront();
progressBar.setVisibility(View.VISIBLE);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(SetWallPaperFullScreenActivity.this);
try
{
wallpaperManager.setBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(SetWallPaperFullScreenActivity.this, "Wallpaper set successfully.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable)
{
Log.d("TAG", "Failed: ");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable)
{
Log.d("TAG", "Prepare Load: ");
}
});
}
Upvotes: 0
Views: 769
Reputation: 86
Try this out,
Create a sticky service as shown below then i have created a TimerTask(Scheduler) which will run after every 24hrs there you can add your code for setting wallpaper. Don't forget to register the service in Manifest. Start this service from any activity.
You can refer the sticky service from here to know more
private Timer timer;
private TimerTask timerTask;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startTimer();
return START_STICKY;
}
public void startTimer() {
timer = new Timer();
initializeTimerTask();
//schedule the timer, after the first **5000ms** the TimerTask will run in every **24hrs**
timer.schedule(timerTask, 5000, 86400000);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
//ToDo set wallapaper here
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
timer = null;
}
}
Upvotes: 0
Reputation: 3936
1) first you need to Schedule job which called every 24 hour. reference link
2) now use below method to set wallpaper
public void setWallpaper {
WallpaperManager myWallpaperManager =
WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.wallpaper);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 2