title: Write Background service in Android
body: <p>My android app has requirement to schedule a task at certain time. It could be daily, weekly, monthly, etc. So while inserting input data, if user selects "daily". There will be back ground service, which will insert same data daily. </p>
  
  <p>I tried to create Service and corresponding receiver class, however it looks like service doesn't run automatically. Can someone tell me what is missing?</p>
  
  <p>Receiver classes   </p>
  
  <pre><code>     public class MyScheduleReceiver extends BroadcastReceiver {
  
        // Restart service every 30 seconds
        private static final long REPEAT_TIME = 1000 * 30;
  
        @Override
        public void onReceive(Context context, Intent intent) {
          AlarmManager service = (AlarmManager) context
              .getSystemService(Context.ALARM_SERVICE);
          Intent i = new Intent(context, MyStartServiceReceiver.class);
          PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
              PendingIntent.FLAG_CANCEL_CURRENT);
          Calendar cal = Calendar.getInstance();
          // Start 30 seconds after boot completed
          cal.add(Calendar.SECOND, 30);
          //
          // Fetch every 30 seconds
          // InexactRepeating allows Android to optimize the energy consumption
          service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
              cal.getTimeInMillis(), REPEAT_TIME, pending);
  
          // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
          // REPEAT_TIME, pending);
          Toast.makeText(context, "Receving", Toast.LENGTH_SHORT).show();
  
        }
  
  
  
     public class MyStartServiceReceiver extends BroadcastReceiver {
  
        @Override
        public void onReceive(Context context, Intent intent) {
          Intent service = new Intent(context, LocalWordService.class);
          context.startService(service);
        }
      } 
  </code></pre>
  
  <p>Android Menifest.xml</p>
  
  <pre><code>   &lt;service
      android:name=".LocalWordService"
      android:label="LocalWordService" &gt;
      &lt;/service&gt;
  
      &lt;receiver android:name="MyScheduleReceiver" &gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  &lt;receiver android:name="MyStartServiceReceiver" &gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>Now when i start application, i call one activity class which basically call the service using below code...but nothing is happening after every 30 seconds.</p>
  
  <pre><code>   Intent mServiceIntent = new Intent(this, LocalWordService.class);
          startService(mServiceIntent);
  </code></pre>
  
  <p>Service class</p>
  
  <pre><code>    public class LocalWordService extends Service {
  private final IBinder mBinder = new MyBinder();
  private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
  @Override
  public void onCreate() {
      // TODO Auto-generated method stub
      super.onCreate();
  
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
  
      Toast.makeText(this, "Starting", Toast.LENGTH_SHORT).show();
      Random random = new Random();
      if (random.nextBoolean()) {
          list.add("Linux");
      }
      if (random.nextBoolean()) {
          list.add("Android");
      }
      if (random.nextBoolean()) {
          list.add("iPhone");
      }
      if (random.nextBoolean()) {
          list.add("Windows7");
      }
      if (list.size() &gt;= 20) {
          list.remove(0);
      }
      return Service.START_NOT_STICKY;
      }
  
    @Override
    public IBinder onBind(Intent arg0) {
      return mBinder;
    }
  
    public class MyBinder extends Binder {
      LocalWordService getService() {
          return LocalWordService.this;
      }
  }
  
  public List&lt;String&gt; getWordList() {
      return list;
  }
  
     }
  </code></pre>
  
  <p>Please advice, what is missing?</p>