Well, here’s the thing. The execute() method of AsyncTask were first introduced to execute serially on a single background thread.

Then it was changed to a pool of threads allowing multiple tasks to operate in parallel beginning with Android Donut OS.

With HoneyComb, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

So if you want to run 2 or more tasks asynchronously, you use the method executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

If your app supports older versions prior to HoneyComb, you can do this kind of check:

So here’s the thing. I wanted my widget to run a task every 12 midnight. The attribute of Intent.ACTION_DATE_CHANGED does not work.

I had read posts in forums that say if you switch back and forth setting the date and time for experimentation to see if the task will run, nothing happens. So the only workaround left is to use an AlarmManager.

Yup, you have to use it rather than being forced to use Intent.ACTION_TIME_TICK which is battery draining as this event will occur every minute.

To set up your AlarmManager to send a broadcast every 12 midnight, do this:

Take note that you have to use getBroadcast() in the PendingIntent class if you want the task in the BroadcastReceiver to be executed. If it is code in a service that you want to run, then call getService().

Related Posts Plugin for WordPress, Blogger...