title: AlarmManager and BroadcastReceiver instead of Service - is that bad ? (Timeout)
body: <p><strong>BACKGROUND INFO:</strong></p>
  
  <p>I need to update some data from the web, about every hour or so, even when my app is closed. The update of the data itself takes about 40 seconds to 1 minute. It is then saved as a Serializable to a file. This file is read when my app starts.</p>
  
  <p><strong>THIS IS THE APPROACH I TOOK FOR THE MOMENT (not using a Service)</strong></p>
  
  <p>use the AlarmManager and BroadcastReceiver like this :</p>
  
  <pre><code>private void set_REFRESH_DATA_Alarm(){
      mContext = Main.this;
      alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      broadcast_intent = new Intent(mContext, 
              RepeatingAlarmReceiver_REFRESH_DATA.class);
      pendingIntent = PendingIntent.getBroadcast(mContext, 0,  broadcast_intent, 0);
      // do a REFRESH every hour, starting for the first time in 30 minutes from now ...
      Calendar now = Calendar.getInstance();
      long triggerAtTime = now.getTimeInMillis()+ (1 * 30 * 60 * 1000); // starts in 30 minutes
      long repeat_alarm_every = (1 * 60 * 60 * 1000); // repeat every 60 minutes
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, 
              repeat_alarm_every, pendingIntent);
  }
  </code></pre>
  
  <p>My <strong>RepeatingAlarmReceiver_REFRESH_DATA.class</strong> takes care of updating the Data from the Web:</p>
  
  <pre><code>public class RepeatingAlarmReceiver_REFRESH_DATA extends BroadcastReceiver {
  
      public static Context mContext;
      ConnectivityManager mConnectivity;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          mContext = context;
          // if Network connection is OK (Wifi or Mobile) then Load data ...
          mConnectivity = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
          Log.i("Hub",
                  "mConnectivity.getNetworkInfo(0)="
                          + mConnectivity.getNetworkInfo(0));
          Log.i("Hub",
                  "mConnectivity.getNetworkInfo(1)="
                          + mConnectivity.getNetworkInfo(1));
          if ((mConnectivity.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
                  || (mConnectivity.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)) {
              Log.i("Hub", "Connectivity OK ...");
              Refresh_HIST_DATA();
          } else {
              // else Show Dialog "No network connection" ...
              Log.i("Hub",
                      "No network connection for the moment... will try again later!");
          }
      }
  
      // =========================================================================
      private void Refresh_HIST_DATA() {
          Log.i("Hub", "Refresh_HIST_DATA()... Starting ...");
          // etc...
      }
  }
  </code></pre>
  
  <p>In the Manifest I have :</p>
  
  <pre><code>&lt;receiver android:name="com.cousinHub.myapp.RepeatingAlarmReceiver_REFRESH_DATA" android:process=":remote" /&gt;
  </code></pre>
  
  <p><strong>PROBLEM :</strong></p>
  
  <p>The alarm gets fired on time and the update starts but then after about 10 seconds it stops (Timeout):</p>
  
  <blockquote>
    <p>06-25 11:55:05.278:
    WARN/ActivityManager(76): Timeout of
    broadcast BroadcastRecord{44bb4348
    null} -
    receiver=android.os.BinderProxy@44bcc670</p>
    
    <p>06-25 11:55:05.278:
    WARN/ActivityManager(76): Receiver
    during timeout: ResolveInfo{44bb42c0
    com.cousinHub.myapp.RepeatingAlarmReceiver_REFRESH_DATA
    p=0 o=0 m=0x0}</p>
    
    <p>06-25 11:55:05.278: INFO/Process(76):
    Sending signal. PID: 819 SIG: 9</p>
    
    <p>06-25 11:55:05.298:
    INFO/ActivityManager(76): Process
    com.cousinHub.myapp:remote (pid 819)
    has died.</p>
  </blockquote>
  
  <p><em>ps: strangely enough, this "Timeout" does not happen after about 10 seconds on my HTC Hero (still on Android 1.5 - API Level 4) but well on my Nexus One (2.1-update1)</em></p>
  
  <p><strong>Questions :</strong></p>
  
  <ol>
  <li>Why this timeout ? Any easy way to avoid this ? </li>
  <li>Did I set up my BroadcastReceiver correctly in the manifest ? Do I need to add something (to avoid this timeout) ?</li>
  <li>Should I absolutely go for a Service for this kind of "Refresh from Web" functionality ? (considering this article : <a href="http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/" rel="noreferrer">http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/</a>) 
  If YES (I should switch to a service): Any good snippets of code/tutorial for this ...</li>
  </ol>
  
  <p>As allways, thanks for your help.</p>
  
  <p>H.</p>