Android obtains the battery status and android battery

Source: Internet
Author: User

Android obtains the battery status and android battery

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/btn_battery" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Get battery information"/> <TextView android: id = "@ + id/TV _battery" android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </RelativeLayout>
Package com. example. yanlei. yl; 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; p Ublic 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 () {// obtain all objects in the layout. btnBattery = (Button) findV IewById (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. addAc Tion (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: // BatteryManager. 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 );}}

 

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.