0

What is the best way to go about running code immediately on system date change?

Currently I am trying to set the wallpaper on a device based on the date. If the date changes, I will set the wallpaper to a new image. My code for getting and setting the wallpaper all works. I am trying to setup an alarm manager but I can't get it to work. How can I make sure my alarm is set to set the wallpaper each time the date changes and immediately.

public void onReceive(Context context, Intent intent)
        {
            // TODO Auto-generated method stub
            if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

                alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
                Intent intent1 = new Intent(context, AlarmReciever.class);
                alarmIntent = PendingIntent.getBroadcast(context, 0, intent1, 0);

                alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY,
                        AlarmManager.INTERVAL_DAY, alarmIntent);

                // Execute DownloadImage AsyncTask
                new DownloadImage().execute(URL);

                try {
                    WallpaperManager.getInstance(context).setBitmap(bm);
                }
                catch (  IOException e) {

                }

            }


         }
user3911976
  • 65
  • 2
  • 10

1 Answers1

0

Register you broadcast receiver in manifest file with this action:

android:action="ACTION_DATE_CHANGED"

Read the doc here for more.

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • Alright I have it detecting system date change now but I am getting an error trying to run my code – user3911976 Feb 27 '15 at 17:46
  • is there a way to set the wallpaper on a device from OnReceive()? My async task works in my activity but I want to set the wallpaper when the action change is noticed by the device without opening the application. – user3911976 Feb 27 '15 at 17:49
  • Start a service in onReceive() of BroadcastRecceiver. Services owrk in the background. Read more here: http://stackoverflow.com/questions/9732812/android-app-to-change-wallpaper-at-regular-intervals-using-timer – denvercoder9 Feb 27 '15 at 20:14
  • Thanks. very helpful advice. I will check this out so I will use alarm manager and then on receive I will run my service. – user3911976 Feb 27 '15 at 22:11
  • If my first answer answered your first question then please mark it as "correct" :) – denvercoder9 Feb 27 '15 at 22:40
  • I still haven't been able to get anything to work. My main activity when you open the application it works. It sets the wallpaper automatically but when I try to set up alarm manager and broadcast receiver. I can get it to show me a message onreceive but when I try to run my async task it fails. – user3911976 Mar 01 '15 at 03:44
  • Don't run an asynctask. In onReceive of broadcast receiver start a Servicce. In the Service class do the wallpaper changing tasks. – denvercoder9 Mar 01 '15 at 12:46