Home>

Principle overview:

Obtaining cell phone battery power is also commonly used in application development.The message that the battery level of the mobile phone in the android system is changed is implemented through intent broadcasting.Common intent actions are intent.action_battery_changed (when the battery level changes), intent.action_battery_low (when the battery level reaches the lower limit), and intent.action_battery_okay (when the battery level is restored from low to high).

When you need to get information about the battery level in your program,The broadcastreceiver component needs to be registered for the application. When a specific action event occurs,A corresponding broadcast will be issued,The application can receive broadcasts via broadcastreceiver,And deal with it accordingly.

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <togglebutton android:id="@ + id/tb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:texton="Stop getting battery information"
    android:textoff="Get battery information" />
  <textview android:id="@ + id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</linearlayout>

batteryactivity class:

package com.ljq.activity;
import android.app.activity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.widget.compoundbutton;
import android.widget.textview;
import android.widget.togglebutton;
import android.widget.compoundbutton.oncheckedchangelistener;
public class batteryactivity extends activity {
  private togglebutton tb=null;
  private textview tv=null;
  private batteryreceiver receiver=null;
  @override
  public void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
    receiver=new batteryreceiver ();
    tv=(textview) findviewbyid (r.id.tv);
    tb=(togglebutton) findviewbyid (r.id.tb);
    tb.setoncheckedchangelistener (new oncheckedchangelistener () {
      public void oncheckedchanged (compoundbutton compoundbutton, boolean ischecked) {
        //Get battery power
        if (ischecked) {
          intentfilter filter=new intentfilter (intent.action_battery_changed);
          registerreceiver (receiver, filter);//Register broadcastreceiver
        } else {
          //Stop getting battery power
          unregisterreceiver (receiver);
          tv.settext (null);
        }
      }
    });
  }
  private class batteryreceiver extends broadcastreceiver {
    @override
    public void onreceive (context context, intent intent) {
      int current=intent.getextras (). getint ("level");//Get the current battery
      int total=intent.getextras (). getint ("scale");//Get the total power
      int percent=current * 100/total;
      tv.settext ("The current battery is" + percent + "%.");
    }
  }
}

operation result:

  • Previous Talking about Object and Function objects in Javascript
  • Next Generation and use of graphic verification code for Java Web development