Friday, June 29, 2012

Periodically Update Data from Server in Background


Update data from server at regular time intervals in background process using AlarmManager, BroadcastReceiver, Service and Notification Manager.

First Activate AlarmManager. Write below code in Activity class

public class MainActivity extends ListActivity {
 
  private static final long REPEAT_TIME = 1000 * 30;
 
        @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setRecurringAlarm(this); }
        
        private void setRecurringAlarm(Context context) {

            Calendar updateTime = Calendar.getInstance();
            updateTime.setTimeZone(TimeZone.getDefault());
            updateTime.set(Calendar.HOUR_OF_DAY, 12);
            updateTime.set(Calendar.MINUTE, 30);
            Intent downloader = new Intent(context, MyStartServiceReceiver.class);
            downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader,       PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
            
            Log.d("MyActivity", "Set alarmManager.setRepeating to: " + updateTime.getTime().toLocaleString());
       
      }

}


First create BroadcastReceiver Class
public class MyStartServiceReceiver extends BroadcastReceiver { 
     @Override
     public void onReceive(Context context, Intent intent) {
            Intent dailyUpdater = new Intent(context, MyService.class); 
            context.startService(dailyUpdater);
            Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
    } 
}

When application is closed or is in background, periodically fetch data from server and show notification on status bar.

Create Service

public class MyService extends IntentService {
    public MyService() {
       super("MyServiceName");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("MyService", "About to execute MyTask");
        new MyTask().execute();
        this.sendNotification(this);
    }
    private class MyTask extends AsyncTask<String, Void, Boolean> {
        @Override 
         protected Boolean doInBackground(String... strings) {
                Log.d("MyService - MyTask", "Calling doInBackground within MyTask");
               return false;
        } 
 }        
private void sendNotification(Context context) {
        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification =  new Notification(android.R.drawable.star_on, "Refresh", System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, "Title","Content", contentIntent);
        notificationMgr.notify(0, notification);
     }
}

Don't forget write below lines in AndroidManifest.xml File 
<service android:name="MyService" ></service> 
<receiver android:name="MyStartServiceReceiver" ></receiver>



11 comments:

  1. Not clear, please explain the process.

    ReplyDelete
  2. Very well done. Thanks.

    **REPEAT_TIME can be discarded(?)

    ReplyDelete
  3. Thanks a lot. Implemented in couple of minutes.

    ReplyDelete
  4. Thanks a lot, your tutorial is very interested can you send me all the code source.

    Thanks in advance.
    Lotfi

    ReplyDelete
  5. I really don't understand why do we use AsyncTask in IntentServise? onHandleIntent method is invoked on the worker thread with a request to process. So I'm pretty sure that we don't need MyTask at all. And should do all routine in onHandleIntent. Please, correct me if I'm wrong.

    ReplyDelete
  6. in this page you helped me, thank you very much for your great contribution:

    https://stackoverflow.com/questions/11737607/how-to-set-the-image-from-drawable-dynamically-in-android/11737709#11737709?newreg=f8073268bc554e82b84e31eef9969fe1

    ReplyDelete