title: Alarm Manager is not working in Xiaomi phones
body: <p>I have tried most of the solution provided on StackOverflow and google about <code>AlarmManager</code> for Marshmallow OS. But I didn;t get the solution. </p>
  
  <p>Code:</p>
  
  <pre><code>AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  
          Intent alarmIntent = new Intent(context, UpdateTables.class);
  
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          Calendar alarmStartTime = Calendar.getInstance();
  
          //midnight
          alarmStartTime.set(Calendar.HOUR_OF_DAY, 0);
          alarmStartTime.set(Calendar.MINUTE, 1);
          alarmStartTime.set(Calendar.SECOND, 0);
  
  if (Calendar.getInstance().getTimeInMillis() &gt; alarmStartTime.getTimeInMillis()) {
              Log.i("Current time ", Constants.getDate(Calendar.getInstance().getTimeInMillis(),"yyyy-MM-dd HH:mm:ss.SSS"));
              alarmStartTime.add(Calendar.DATE, 1);
          }
  
   alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMills(), pendingIntent);
  </code></pre>
  
  <p><strong>BroadcastReceiver</strong></p>
  
  <pre><code>public class UpdateTables extends WakefulBroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
  }
  }
  </code></pre>
  
  <p>When I am closing the app then the set alarm is removed from the scheduled alarm list:</p>
  
  <pre><code> ./adb shell dumpsys alarm 
  </code></pre>
  
  <p>So I have created the service that calls <code>AlarmManager</code> again <code>onTaskRemoved()</code> from recent apps:</p>
  
  <pre><code>public class myService extends Service {
  
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onTaskRemoved(Intent rootIntent) {
  
          setTheTimeToUpdateTables(getApplicationContext());
          super.onTaskRemoved(rootIntent);
      }
  
  }
  </code></pre>
  
  <p>Solution Tried:</p>
  
  <ol>
  <li>Add the app to the autostart list.</li>
  <li>Remove the app from battery saver list. </li>
  <li>Use the <code>WakefulBroadcastReceiver</code> instead of <code>BroadCastReciever</code>.</li>
  </ol>