title: Android problems in receiving broadcast from pending intent
body: <p>I am using alarm manager to fire broadcast at specific time. But after testing it so many times, I found sometimes broadcast is received late. Sometimes 5 seconds, 10, 15 or even more sometimes. Specially when device is locked. I have done various experiments. My least problematic code is here.</p>
  
  <p>Even after using wake lock, I don't know what I am lacking.</p>
  
  <blockquote>
    <p><strong>Firing Intent</strong></p>
  </blockquote>
  
  <pre><code>Intent intent = new Intent(this.getApplicationContext(), BroadCastReceiver.class);  
  //..... some extras
  PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), code, intent, 0);
  manager.setRepeating(AlarmManager.RTC_WAKEUP, time, 1000 * 120 , pi);
  </code></pre>
  
  <blockquote>
    <p><strong>Receiving broadcast</strong></p>
  </blockquote>
  
  <pre><code>public void onReceive(Context context, Intent intent)
  {
         WakeLocker.acquire(context);
         .......
         Intent alarm = new Intent(context, xyz.class);
         alarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(alarm);
  }
  </code></pre>
  
  <p>and releasing Wakelock in destroy() of xyz activity.</p>
  
  <blockquote>
    <p><strong>Custom WakeLocker class</strong>
        public abstract class WakeLocker {</p>
  </blockquote>
  
  <pre><code>private static PowerManager.WakeLock wakeLock;
  
  public static void acquire(Context ctx) {
      if (wakeLock != null) wakeLock.release();
  
      PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
      wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
              PowerManager.ACQUIRE_CAUSES_WAKEUP |
              PowerManager.ON_AFTER_RELEASE, "haris");
      wakeLock.acquire();
  }
  
  public static void release() {
      if (wakeLock != null) wakeLock.release(); wakeLock = null;
  }
  </code></pre>
  
  <p>}</p>