Android Battery status monitoring

Source: Internet
Author: User

Android Battery status monitoring

If you want to monitor changes in the battery status, you need to dynamically register: android. intent. action. BATTERY_CHANGED: After receiving the Action, you can obtain the information you need based on the corresponding Key. For more information, see the BatteryChangedReceiver class in the following example.

Package com. example. charginganimation; import android. app. activity; 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. view. view; public class MainActivity extends Activity {private CustomClipLoading ccl; private BatteryChangedReceiver extends er = New BatteryChangedReceiver (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); registerReceiver (ER er, getFilter ();} private IntentFilter getFilter () {IntentFilter filter = new IntentFilter (); filter. addAction (Intent. ACTION_BATTERY_CHANGED); filter. addAction (Intent. ACTION_BATTERY_LOW); filter. addAction (Intent. ACTION_BATT ERY_OKAY); return filter ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubunregisterReceiver (receiver); super. onDestroy ();} class BatteryChangedReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stubfinal String action = intent. getAction (); if (action. specified signorecase (Intent. ACTION_BATTERY_CHANGED )){ System. out. println ("BatteryChangedReceiver BATTERY_CHANGED_ACTION ---"); // The current battery voltage int voltage = intent. getIntExtra (BatteryManager. EXTRA_VOLTAGE,-1); // The Battery health status int health = intent. getIntExtra (BatteryManager. EXTRA_HEALTH,-1); switch (health) {case BatteryManager. BATTERY_HEALTH_COLD: System. out. println ("BATTERY_HEALTH_COLD"); break; case BatteryManager. BATTERY_HEALTH_DEAD: System. out. println ("BATTERY_HEALT H_DEAD "); break; case BatteryManager. BATTERY_HEALTH_GOOD: System. out. println ("BATTERY_HEALTH_GOOD"); break; case BatteryManager. BATTERY_HEALTH_OVERHEAT: System. out. println ("BATTERY_HEALTH_OVERHEAT"); break; case BatteryManager. BATTERY_HEALTH_OVER_VOLTAGE: System. out. println ("BATTERY_HEALTH_COLD"); break; case BatteryManager. BATTERY_HEALTH_UNKNOWN: System. out. println ("BATTERY_HEALTH_UNKNOWN"); break; case Batte RyManager. BATTERY_HEALTH_UNSPECIFIED_FAILURE: System. out. println ("BATTERY_HEALTH_UNSPECIFIED_FAILURE"); break; default: break;} // current battery power, which is between 0 and EXTRA_SCALE. int level = intent. getIntExtra (BatteryManager. EXTRA_LEVEL,-1); // maximum battery power int scale = intent. getIntExtra (BatteryManager. EXTRA_SCALE,-1); // Where is the current mobile phone used power int pluged = intent. getIntExtra (BatteryManager. EXTRA_PLUGGED,-1); switch (pluged) {case Batter YManager. BATTERY_PLUGGED_AC: // the power supply is AC charger. [charger] System. out. println ("BATTERY_PLUGGED_AC"); break; case BatteryManager. BATTERY_PLUGGED_USB: // the power supply is USB portSystem. out. println ("BATTERY_PLUGGED_USB"); break; default: break;} int status = intent. getIntExtra (BatteryManager. EXTRA_STATUS,-1); switch (status) {case BatteryManager. BATTERY_STATUS_CHARGING: // The System is being charged. out. println ("BATTERY_STATUS_CHARGING"); B Reak; case BatteryManager. BATTERY_STATUS_DISCHARGING: System. out. println ("BATTERY_STATUS_DISCHARGING"); break; case BatteryManager. BATTERY_STATUS_FULL: // full System. out. println ("BATTERY_STATUS_FULL"); break; case BatteryManager. BATTERY_STATUS_NOT_CHARGING: // No charging System. out. println ("BATTERY_STATUS_NOT_CHARGING"); break; case BatteryManager. BATTERY_STATUS_UNKNOWN: // unknown status System. out. println ("BATTERY_STATUS_U NKNOWN "); break; default: break;} // battery technology. For example, the lithium battery is Li-ionString technology = intent. getStringExtra (BatteryManager. EXTRA_TECHNOLOGY); // The temperature of the current battery, int temperature = intent. getIntExtra (BatteryManager. EXTRA_TEMPERATURE,-1); System. out. println ("voltage =" + voltage + "technology =" + technology + "temperature =" + temperature + "level =" + level + "scale =" + scale );} else if (action. specified signorecase (Intent. ACTION_BATTERY_LOW) {// indicates that the current battery is low. out. println ("BatteryChangedReceiver ACTION_BATTERY_LOW ---");} else if (action. specified signorecase (Intent. ACTION_BATTERY_OKAY) {// indicates that the current battery has been restored from low power to normal System. out. println ("BatteryChangedReceiver ACTION_BATTERY_OKAY ---");}}}}

Obtain apk and version information

private int getVerCode(Context context) {int verCode = -1;try {verCode = context.getPackageManager().getPackageInfo("com.example.charginganimation", 0).versionCode;} catch (NameNotFoundException e) {e.printStackTrace();}return verCode;}private String getVerName(Context context) {String verName = "";try {verName = context.getPackageManager().getPackageInfo("com.example.charginganimation", 0).versionName;} catch (NameNotFoundException e) {e.printStackTrace();}return verName;}



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.