Android Get battery status

Source: Internet
Author: User

<Relativelayoutxmlns: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" >    <ButtonAndroid:id= "@+id/btn_battery"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Get information on battery" />    <TextViewAndroid:id= "@+id/tv_battery"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"        /></Relativelayout>
 PackageCom.example.yanlei.yl;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.BatteryManager;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsappcompatactivity {//button to define battery information    PrivateButton Btnbattery; //define TextView to display battery information    PrivateTextView Tvbattery; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //get all the objects in the layoutFindview (); //set listener for objectSetlistener (); }    Private voidFindview () {//get all the objects in the layoutBtnbattery =(Button) Findviewbyid (r.id.btn_battery); Tvbattery=(TextView) Findviewbyid (r.id.tv_battery); }    //set listener for object    Private voidSetlistener () {Btnbattery.setonclicklistener (listener); } Onclicklistener Listener=NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub            Switch(V.getid ()) {//the current volume                 Caser.id.btn_battery:intentfilter Filter=NewIntentfilter ();                    Filter.addaction (intent.action_battery_changed);                    Registerreceiver (mbroadcastreceiver, filter);  Break;    }        }    }; //declaring a broadcast recipient object    PrivateBroadcastreceiver Mbroadcastreceiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method StubString action =intent.getaction (); if(Action.equals (intent.action_battery_changed)) {//Get 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: Full power. //Batterymanager.battery_status_unknown: Unknown state.                 intStatus = Intent.getintextra ("status", 0); //Get health status://Batterymanager.battery_health_good: in good condition. //Batterymanager.battery_health_dead: The battery has no electricity. //batterymanager.battery_health_over_voltage: The battery voltage is too high. //Batterymanager.battery_health_overheat: The battery is overheating. //Batterymanager.battery_health_unknown: Unknown state.                 intHealth = Intent.getintextra ("Health", 0); //Boolean type                BooleanPresent = Intent.getbooleanextra ("Present",false); //get the remaining capacity of the battery                intLevel = Intent.getintextra ("level", 0); //get the maximum battery value. is usually 100.                 intScale = Intent.getintextra ("scale", 0); //Get icon ID                intIcon_small = Intent.getintextra ("Icon-small", 0); //Charging method: BatteryManager.BATTERY_PLUGGED_AC:AC charge. BatteryManager.BATTERY_PLUGGED_USB:USB charging.                 intplugged = Intent.getintextra ("Plugged", 0); //get the battery voltage                intVoltage = Intent.getintextra ("voltage", 0); //get the battery temperature, 0.1 degrees units. For example, when you say 197, it means 19.7 degrees.                intTemperature = Intent.getintextra ("Temperature", 0); //get the type of batteryString technology = Intent.getstringextra ("Technology"); //Get battery StatusString statusstring = ""; //get the status string based on the status ID                Switch(status) { CaseBatteryManager.BATTERY_STATUS_UNKNOWN:statusString= "Unknown";  Break;  CaseBatteryManager.BATTERY_STATUS_CHARGING:statusString= "Charging";  Break;  CaseBatteryManager.BATTERY_STATUS_DISCHARGING:statusString= "discharging";  Break;  CaseBatteryManager.BATTERY_STATUS_NOT_CHARGING:statusString= "Not Charging";  Break;  CaseBatteryManager.BATTERY_STATUS_FULL:statusString= "Full";  Break; }                //get the battery life statusString healthstring = ""; //get battery life based on the status ID                Switch(health) { CaseBatteryManager.BATTERY_HEALTH_UNKNOWN:healthString= "Unknown";  Break;  CaseBatteryManager.BATTERY_HEALTH_GOOD:healthString= "good";  Break;  CaseBatteryManager.BATTERY_HEALTH_OVERHEAT:healthString= "Overheat";  Break;  CaseBatteryManager.BATTERY_HEALTH_DEAD:healthString= "Dead";  Break;  CaseBatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:healthString= "Voltage";  Break;  CaseBatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:healthString= "Unspecified Failure";  Break; }                //Get charging ModeString acstring = ""; //get charging mode based on charge status ID                Switch(plugged) { CaseBatteryManager.BATTERY_PLUGGED_AC:acString= "Plugged AC";  Break;  CaseBatteryManager.BATTERY_PLUGGED_USB:acString= "Plugged USB";  Break; }                //Display battery InformationTvbattery.settext ("Battery Status:" +statusstring+ "\ n Health Value:" +healthstring+ "\ n Battery remaining capacity:" + Level+ "\ nthe maximum battery 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);    }        }    }; @Overrideprotected voidOnPause () {Super. OnPause (); //Unlock Registration MonitoringUnregisterreceiver (Mbroadcastreceiver); }}

Android Get battery status

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.