Skip to content Skip to sidebar Skip to footer

Doing Http Request From Android Using Timer

I want to write android chat application and I want to know is it good practice to do periodical requests to web server for getting new messages using android Timer by opening ne

Solution 1:

Why not but if you want your chat application to be able to send notifications of new messages to the user while screen is off then you'd implement the timer in a service, maybe more via an alarm that sets periodic requests to the server :

  • set alarm (inexact repeat) to send an intent to broadcast receiver.
  • broadcast receiver starts a service.
  • service starts an AsyncTask to request the web server.
  • service send (or not) a notification to the user if there are any new messages.
  • service stops itself.

Solution 2:

The good style is if you will use AlarmManager instead of simple Timer. In AlarmManager task you should start your service to execute sync operation (in simple case you can use IntentService). After the operation is finished, service will post new sync task to AlarmManager.

Post a Comment for "Doing Http Request From Android Using Timer"