Search code examples
javaandroidmysqlservicebroadcastreceiver

Notifying app after an entry or update in remote database in android


I m new to Android and have developed an app that inserts and update data into remote
MySql database and i m able to display it in list view also using JSON object. Now i want notify app when ever i insert or update data in database using background services even though less efficient than GCM.

   public class Myservice extends Service
  {
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) 
   {
    //TODO do something useful
    return Service.START_NOT_STICKY;
   }

   @Override
   public IBinder onBind(Intent intent) 
   {
   //TODO for communication return IBinder implementation
   return null;
   }
 } 

I referred some links and vogella tutorials but it is for alarm manager.I need for database Can somebody provide some code examples.


Solution

  • Running a background task and polling your server for update is a very bad idea, as it has pleanty of drawbacks. I would advice you to use GCM. As its very easy to implement.(GCM with php and GCM with java)

    However, Since you want a service based approach:

    Try using the alarm managers, as they can save you on power.

    1. Set up an AlarmManager to fire in say 10 minutes. In response to the
    2. alarm, start a service that polls the data. After polling it should
    3. set itself up with a new Alarm to fire again in another 10 minutes.
    4. The service shuts itself down.

    Note:
    I will still urge you to use gcm as they are very efficient at doing that one thing(polling for updates).