Andriod obtains the battery information and andriod obtains the battery information.
<? Xml version = "1.0"?> <LinearLayout android: orientation = "vertical" android: layout_height = "match_parent" android: layout_width = "match_parent" xmlns: tools = "http://schemas.android.com/tools" xmlns: android = "http://schemas.android.com/apk/res/android"> <Button android: layout_height = "wrap_content" android: layout_width = "match_parent" android: text = "Get battery information" android: id = "@ + id/btn_battery"/> <TextView android: layout_height = "wrap_content" android: layout_width = "match_parent" android: id = "@ + id/TV _battery"/> </LinearLayout>
Package com. example. yanlei. wifi; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. batteryManager; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; Public class MainActivity extends AppCompatActivity {// define the battery Button private Button btnBattery; // defines the textview private TextView tvBattery that displays battery information; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain all objects in the layout. findView (); // set the object listener setListener ();} private void findView () {// get all objects in the layout btnBattery = (Button) fin DViewById (R. id. btn_battery); tvBattery = (TextView) findViewById (R. id. TV _battery);} // sets the object listener private void setListener () {btnBattery. setOnClickListener (listener);} OnClickListener listener = new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {// current volume case R. id. btn_battery: IntentFilter filter = new IntentFilter (); filter. add Action (Intent. ACTION_BATTERY_CHANGED); registerReceiver (listener, filter); break ;}}; // declare the broadcast receiver object private BroadcastReceiver receiver = new BroadcastReceiver () {@ Override public void onReceive (Context context Context, intent intent) {// TODO Auto-generated method stub String action = intent. getAction (); if (action. equals (Intent. ACTION_BATTERY_CHANGED) {// obtain the battery status: // BatteryManag Er. BATTERY_STATUS_CHARGING: charging status. // BatteryManager. BATTERY_STATUS_DISCHARGING: discharge status. // BatteryManager. BATTERY_STATUS_NOT_CHARGING: not full. // BatteryManager. BATTERY_STATUS_FULL: fully charged. // BatteryManager. BATTERY_STATUS_UNKNOWN: Unknown Status. Int status = intent. getIntExtra ("status", 0); // get the health status: // BatteryManager. BATTERY_HEALTH_GOOD: The status is good. // BatteryManager. BATTERY_HEALTH_DEAD: The BatteryManager has no power. // BatteryManager. BATTERY_HEALTH_OVER_VOLTAGE: the battery voltage is too high. // BatteryManager. BATTERY_HEALTH_OVERHEAT: The BatteryManager is overheated. // BatteryManager. BATTERY_HEALTH_UNKNOWN: Unknown Status. Int health = intent. getIntExtra ("health", 0); // boolean type boolean present = intent. getBooleanExtra ("present", false); // get the remaining battery capacity int level = intent. getIntExtra ("level", 0); // obtain the maximum battery value. Usually 100. Int scale = intent. getIntExtra ("scale", 0); // get the icon ID int icon_small = intent. getIntExtra ("icon-small", 0); // charging method: BatteryManager. BATTERY_PLUGGED_AC: AC is charged. BatteryManager. BATTERY_PLUGGED_USB: USB charging. Int plugged = intent. getIntExtra ("plugged", 0); // obtain the battery voltage int voltage = intent. getIntExtra ("voltage", 0); // obtain the temperature of the battery, in 0.1 degrees. For example, if the value is 197, it means 19.7 degrees int temperature = intent. getIntExtra ("temperature", 0); // obtain the battery type String technology = intent. getStringExtra ("technology"); // obtain the battery status String statusString = ""; // obtain the status String switch (status) {case BatteryManager Based on the status id. BATTERY_STATUS_UNKNOWN: statusString = "unknown"; break; case BatteryManager. BATTERY_STATUS_CHARGING: statusString = "charging"; break; case BatteryManager. BATTERY_STATUS_DISCHARGING: statusString = "discharging"; break; case BatteryManager. BATTERY_STATUS_NOT_CHARGING: statusString = "not charging"; break; case BatteryManager. BATTERY_STATUS_FULL: statusString = "full"; break;} // obtain the battery life status String healthString = ""; // obtain the battery life switch (health) based on the Status id) {case BatteryManager. BATTERY_HEALTH_UNKNOWN: healthString = "unknown"; break; case BatteryManager. BATTERY_HEALTH_GOOD: healthString = "good"; break; case BatteryManager. BATTERY_HEALTH_OVERHEAT: healthString = "overheat"; break; case BatteryManager. BATTERY_HEALTH_DEAD: healthString = "dead"; break; case BatteryManager. BATTERY_HEALTH_OVER_VOLTAGE: healthString = "voltage"; break; case BatteryManager. BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthString = "unspecified failure"; break;} // obtain the charging mode String acString = ""; // obtain the charging mode switch (plugged) based on the charging status id) {case BatteryManager. BATTERY_PLUGGED_AC: acString = "plugged ac"; break; case BatteryManager. BATTERY_PLUGGED_USB: acString = "plugged usb"; break;} // display battery information. setText ("Battery status:" + statusString + "\ n health:" + healthString + "\ n battery remaining capacity:" + level + "\ n Battery maximum value: "+ scale +" \ n small icon: "+ icon_small +" \ n charging method: "+ plugged +" \ n charging method: "+ acString +" \ n battery voltage: "+ voltage +" \ n battery temperature: "+ (float) temperature * 0.1 +" \ n battery type: "+ technology );}}}; @ Override protected void onPause () {super. onPause (); // unregister listener unregisterReceiver (mBroadcastReceiver );}}