Android mobile phone battery usage

Source: Internet
Author: User

Principle Overview:

The acquisition of cell phone battery power is also frequently used in application development. In Android systems, messages that change cell phone battery power are broadcast using Intent, and commonly used Intent actions include Intent. ACTION_BATTERY_CHANGED (when the battery power changes), Intent. ACTION_BATTERY_LOW (battery limit reached), and Intent. ACTION_BATTERY_OKAY (battery recovery from low to high ).

When you need to obtain battery information in the program, you need to register the BroadcastReceiver component for the application. When a specific Action event occurs, the system will broadcast it accordingly, applications can use BroadcastReceiver to receive broadcasts and process them 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 obtaining power information"
Android: textOff = "get power 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 batterypolicer extends ER = 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 ){
// Obtain battery power
If (isChecked ){
IntentFilter filter = new IntentFilter (Intent. ACTION_BATTERY_CHANGED );
RegisterReceiver (receiver, filter); // register BroadcastReceiver
} Else {
// Stop obtaining 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"); // obtain the current power
Int total = intent. getExtras (). getInt ("scale"); // get the total power
Int percent = current * 100/total;
TV. setText ("current power is" + percent + "%. ");
}
}


}

Running result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.