android--Battery-related information acquisition

Source: Internet
Author: User

1. Define broadcast receive, display battery information--batteryinfobroadcastreceiver

Package Org.lxh.demo;import Android.app.alertdialog;import Android.app.dialog;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.dialoginterface;import Android.content.intent;public class Batteryinfobroadcastreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {if (Intent.ACTION_BATTERY_CHANGED.equals (Intent.getaction ())) {int level = Intent.getintextra ("level", 0), int scale = Intent.getintextra ("scale", 0), int voltage = Intent.getintextra ("voltage", 0 ) int temperature = Intent.getintextra ("Temperature", 0); String technology = Intent.getstringextra ("technology");D ialog Dialog = new Alertdialog.builder (context). Settitle (" Battery power "). Setmessage (" Battery charge: "+ string.valueof (Level * 100/scale) +"%\n "+" battery voltage: "+ string.valueof ((float) voltage/1000) + "V" + "\ n Battery Type:" + technology + "\ n" + "Battery temperature:" + string.valueof ((float) temperature/10) + "°c"). Setnegativebutton ("Off", n EW Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface arg0,int arg1) {}}). Create ();d ialog.show ();}} 

2. Define the layout manager--main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/mybtn"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Get battery power"/></linearlayout>

3. Define the Activity program:

Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Hello extends Activity {private Button mybtn = Null;public void OnCreate (Bundle saved Instancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use THIS.MYBTN = (Button) Super.findviewbyid (R.ID.MYBTN); This.mybtn.setOnClickListener (new Onclicklistenerimpl ());} Private class Onclicklistenerimpl implements Onclicklistener {public void OnClick (View v) {Batteryinfobroadcastreceiver Receiver = Null;receiver = new Batteryinfobroadcastreceiver (); intentfilter filter = new Intentfilter (Intent.action_battery_changed); Hello.this.registerReceiver (receiver, filter);}}}

The effect of running on the actual phone is as follows:


Source: http://download.csdn.net/detail/yayun0516/8409715

android--Battery-related information acquisition

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.