2

I want to change the device's background each specific interval (30 seconds for example) . I've been searching for days for a similar project or tutorial but I didn't find anything helpful . As I guess the app that I'm going to code will be a service since I want it to run in the background . I have the background images included in the drawable folder . So can any one help ?? and thanks in advance

AlphaCode
  • 439
  • 1
  • 3
  • 17

1 Answers1

3

There is a great application that does this and it's opensource. It's called Muzei - link created by Roman Nurik. Once a day, this application gets wallpapers from Internet and change your background. You can totally get this code and modify the frequency of the background change, and strip all the web calls and redirect to your internal images

You can also code a plugin for Muzei. You will only have a little part of the code to make it work. It will be a lot easier. But, in the other hand, you will have less control on the time between every wallpaper change.

For your code it will be a mix between :
AlarmManager run every hour

// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += remainingMilisecondsToTopHour;
long a=c.getTimeInMillis();

// Schedule the alarm!
AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,
c.getTimeInMillis(), 1*60*60*1000, sender);

and Changing Wallpaper. Do not forget to add the permission of changing background in your manifest

// to set a background we need to use bitmap  
InputStream is = getResources().openRawResource(R.Drawable.myImage);  

// we set the phone background to that image.  
Bitmap bm = BitmapFactory.decodeStream(is);  

try {  
     getApplicationContext().setWallpaper(bm);  
     //  add permission of background from manifest file  
} catch (IOException e) {  
     // TODO Auto-generated catch block  
     e.printStackTrace();  
}

Using a service in background all the time will use way more battery of your device.

Run one task every hour if you still want to use a service ;)

Finally you should register to the broadcast event Screen On/Off to avoid changing wallpaper while the device is idle and draining the battery for nothing

Community
  • 1
  • 1
Vincent D.
  • 977
  • 7
  • 20
  • Thanks Vincent for this help . I'll look for it and if it worked with me I'll mark it as the answer . Thanks again – AlphaCode Nov 12 '14 at 14:06
  • looks to me as if you are setting the alarm to 1h – Dawnkeeper Nov 12 '14 at 15:52
  • Yes it's every hour. The question was about "Changing device background each specific interval" even if he said 30 seconds by example, The link title above specify it "AlarmManager run everyhour" – Vincent D. Nov 12 '14 at 15:54
  • can someone help me and give me the code please i have task to do that changing app once a day :( i can't find anything in the net –  Oct 21 '16 at 06:33