title: Android AlarmManager.setExactAndAllowWhileIdle() and WakefulBroadcastReceiver Not working in some new manufactured and low price devices
body: <p>I think this question is asked many time in <code>stackoverflow</code> but still so many people struggling to resolved it.</p>
  
  <p>In my android app I have to wake up device for every half hour to getting current location and send it to server. For this I have used <code>AlarmManager</code> with <code>setExactAndAllowWhileIdle()</code> method and <code>WakefulBroadcastReceiver</code>. Its working fine in almost all standard/popular devices like samsung, LG(Nexus), Sony, Panasonic, lenovo, Motorola, Micro max and so on....But some other devices mostly china devices not supporting or cannot allow device wake up from doze mode with <code>setExactAndAllowWhileIdle()</code>. I have tested it in <code>leeco letV (Android OS 6.1)</code> device which not allow alarm manager to wake up in specific interval.</p>
  
  <p>My code portion I have mentioned below :</p>
  
  <pre><code>UserTrackingReceiverIntentService.java
  
  public class UserTrackingReceiverIntentService extends IntentService {
  
      public static final String TAG = "UserTrackingReceiverIntentService";
      Context context;
      public UserTrackingReceiverIntentService() {
          super("UserTrackingReceiverIntentService");
      }
  
      @TargetApi(Build.VERSION_CODES.M)
      @Override
      protected void onHandleIntent(Intent intent) {
          this.context = this;
  
          if (!Util.isMyServiceRunning(LocationService.class, context)) {
              context.startService(new Intent(context, LocationService.class));
          }
  
          Calendar calendar = Calendar.getInstance();
          //********************************** SETTING NEXT ALARM *********************************************
              Intent intentWakeFullBroacastReceiver = new Intent(context, SimpleWakefulReceiver.class);
              PendingIntent sender = PendingIntent.getBroadcast(context, 1001, intentWakeFullBroacastReceiver, 0);
              AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
  
          if (calendar.get(Calendar.MINUTE) &gt;= 0 &amp;&amp; calendar.get(Calendar.MINUTE) &lt; 30) {
              calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
              calendar.set(Calendar.MINUTE, 30);
              calendar.set(Calendar.SECOND, 0);
          } else if (calendar.get(Calendar.MINUTE) &gt;= 30) {
              if (calendar.get(Calendar.HOUR_OF_DAY) == 23) {
                  calendar.set(Calendar.HOUR_OF_DAY, 0);
                  calendar.add(Calendar.DAY_OF_MONTH, 1);
              } else {
                  calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);
              }
              calendar.set(Calendar.MINUTE, 0);
              calendar.set(Calendar.SECOND, 0);
          } else {
              calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
              calendar.set(Calendar.MINUTE, 0);
              calendar.set(Calendar.SECOND, 0);
          }
  
          //MARSHMALLOW OR ABOVE
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                      calendar.getTimeInMillis(), sender);
          }
          //LOLLIPOP 21 OR ABOVE
          else if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
              AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), sender);
              alarmManager.setAlarmClock(alarmClockInfo, sender);
          }
          //KITKAT 19 OR ABOVE
          else if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {
              alarmManager.setExact(AlarmManager.RTC_WAKEUP,
                      calendar.getTimeInMillis(), sender);
          }
          //FOR BELOW KITKAT ALL DEVICES
          else {
              alarmManager.set(AlarmManager.RTC_WAKEUP,
                      calendar.getTimeInMillis(), sender);
          }
  
  
          Util.registerHeartbeatReceiver(context);
          SimpleWakefulReceiver.completeWakefulIntent(intent);
      }
  }
  </code></pre>
  
  <p>Every time above service set next alarm after 30 min once it called.</p>
  
  <pre><code>public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          // This is the Intent to deliver to our service.
          Calendar calendar = Calendar.getInstance();
          Intent service = new Intent(context, UserTrackingReceiverIntentService.class);
          Date date = new Date();
  
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
          String DateTime = sdf.format(date);
          DateTime = Util.locatToUTC(DateTime);
          service.putExtra("date", String.valueOf(DateTime));
  
          String latitude = Util.ReadSharePrefrence(context, "track_lat");
          String longitude = Util.ReadSharePrefrence(context, "track_lng");
  
          service.putExtra("lat", latitude);
          service.putExtra("lon", longitude);
  
          Log.i("SimpleWakefulReceiver", "Starting service @ " + calendar.get(Calendar.HOUR_OF_DAY) + " : " + calendar.get(Calendar.MINUTE) + " : " + calendar.get(Calendar.SECOND));
  
          // Start the service, keeping the device awake while it is launching.
          startWakefulService(context, service);
      }
  }
  </code></pre>
  
  <p>As I have changed some setting manually from <code>leeco letV</code> device like Settings > Battery > Align Wakeup > Disabled both the options. If it enabled battery can ignore force wake ups from apps.</p>
  
  <p>My Questions:</p>
  
  <ol>
  <li><p>What happened in such kind of devices? Why they not allow alarm to fire in specific time interval?</p></li>
  <li><p>In some devices like Gionee s plus alarm firing 2-3 minutes late... why? </p></li>
  <li><p><code>android.net.conn.CONNECTIVITY_CHANGE</code> broad cast receiver to listen when network connectivity change .....What to do when it is not working in some devices like Gionee s plus?</p></li>
  <li><p>There are many variation for battery optimization setting for various manufacturer....Is it can harm to our app's background services if is then what the solution for this.</p></li>
  </ol>