title: Call web service in interval on background
body: <p>Application need synchronize data from server in interval (for example every 30min) or manually on background. Result is saved to local database. After sync is done I want remind activity/fragment ... and force update list (if needed). There are many activities, so I want move it outside of activity and make it more consistent.</p>
  
  <p>For now I created AsyncTask which get result from server and save to DB.</p>
  
  <p>What shoud I use? BroadcastReciever, Service, AlarmManager?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Based on answers I start <strong>alarm</strong> in Application</p>
  
  <pre><code>AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, AlarmReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  manager.setInexactRepeating(AlarmManager.RTC, 0, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
  </code></pre>
  
  <p>Created <strong>Receiver</strong></p>
  
  <pre><code>public class AlarmReceiver extends WakefulBroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent syncIntent = new Intent();
          syncIntent.setClass(context, DataSyncer.class);
          startWakefulService(context, syncIntent);
      }
  }
  </code></pre>
  
  <p>Created <strong>IntentService</strong></p>
  
  <pre><code>public class DataSyncer extends IntentService {
          @Override
          protected void onHandleIntent(Intent intent) {
              // get data from server
              // save to DB
              AlarmReceiver.completeWakefulIntent(intent);
          }
  }
  </code></pre>
  
  <p>And registered Receiver and Service in <strong>AndroidManifest</strong></p>
  
  <pre><code>&lt;service
      android:name="com.cloudit.tsystems.app.DataSyncer"
      android:enabled="true"&gt;
  &lt;/service&gt;
  &lt;receiver
      android:name="com.cloudit.tsystems.app.AlarmReceiver"
      android:enabled="true"&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>Where and how I notify that sync is done in Activity/Fragment?</p>