title: Proper optimized way to monitor battery level in android
body: <p>I want to monitor the battery level and if it reaches certain level, i want to DO-SOMETHING. <strong>What I am worried about is draining too much battery unnecessarily.</strong></p>
  
  <p>Using a service will keep on running in the background. Also if i used android.intent.action.BATTERY_CHANGED intent, it will check for every single change in the battery level. This two methods might drain unnecessary battery according to my requirements.</p>
  
  <p>So, if I use AlarmManager(which is also a service handled by OS), it will check that battery level every user-defined(appropriate time).</p>
  
  <p>The following are codes which I have implemented.</p>
  
  <pre><code>import android.app.Activity;
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  
  public class MainActivity extends Activity {
  
  private AlarmManager mAlarmManager;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_main);
  
          mAlarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  
          Intent intentAlarm = new Intent(this, AlarmReceiverTest.class);
  
          // User defined time
          long fiveMin= (AlarmManager.INTERVAL_FIFTEEN_MINUTES/3); // adjustment here
  
          mAlarmManager.setRepeating(AlarmManager.RTC,
                      System.currentTimeMillis()+fiveMin, oneMin, 
                      PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
  
      }
  
  }
  </code></pre>
  
  <p>Broadcast receiver:</p>
  
  <pre><code>public class AlarmReceiverTest extends BroadcastReceiver {
  
  @Override
  public void onReceive(Context pContext, Intent arg1) {
  
      IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      Intent batteryStatus = pContext.registerReceiver(null, ifilter);
  
      int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
      int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
      int percent = (level*100)/scale;
  
      if(percent== myThreshold)
          //DO-SOMETHING
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In manifest:</p>
  
  <pre><code>&lt;receiver android:name="AlarmReceiverTest"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.BATTERY_CHANGED"/&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>Permission:</p>
  
  <pre><code>android.permission.BATTERY_STATS
  </code></pre>
  
  <p>Since its a repeating alarm, it will keep on checking even if the application is closed.</p>
  
  <p>eg.: DO-SOMETHING: might be lock the application if the application was already opened <strong>OR</strong> not allowed to open the application if it was already closed.</p>
  
  <p><strong>Is this the proper way to do and is my understanding correct?</strong>
  Kindly suggest me if there is any other proper way to do.</p>
  
  <p><strong>EDITED:</strong></p>
  
  <p><strong>If I were to check the battery level for every 5-10 hours of interval, what would be the best way to do so?</strong></p>