:
Activity Code:
Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. app. dialog; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. intentfilter; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br /> Import android. view. window; <br/> Import android. view. windowmanager; <br/> Import android. view. view. onclicklistener; <br/> Import android. view. viewgroup. layoutparams; <br/> Import android. widget. button; <br/> Import android. widget. textview; <br/> Import android. widget. toast; <br/>/*** <br/> * @ Description: Check the battery of the mobile phone. Listen to intent here. the broadcast receiver of action_battery_changed. It can only be <br/> * registered in the program, and cannot be androimanifest. XML Static row registration <br/> * @ author chenzheng_java <br/> * @ since 2011/03/16 <br/> */<br/> public class batteryactivity extends Activity implements onclicklistener {</P> <p> // This action is broadcast when the system is in the charging status or the power usage changes. <br/> Private Static final string action_name = intent. action_battery_changed; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> set Contentview (R. layout. battery); </P> <p> button = (button) findviewbyid (R. id. button1); <br/> // note that the activity here implements the view. onclicklistener interface <br/> button. setonclicklistener (this); </P> <p >}</P> <p> // declare the broadcast receiver <br/> broadcastreceiver = new broadcastreceiver () {</P> <p> @ override <br/> Public void onreceive (context, intent) {<br/> string actionname = intent. getaction (); <Br/> If (actionname. equals (action_name) {<br/> int level = intent. getintextra ("level", 0); <br/> int scale = intent. getintextra ("scale", 100); <br/> // toast. maketext (batteryactivity. this, "level =" + level + "scale =" + scale, toast. length_long ). show (); <br/> final dialog = new dialog (batteryactivity. this); <br/> dialog. settitle ("power display"); </P> <p> // load the XML layout file as a view object <br/> layoutinflater flater = layoutinf Later. from (batteryactivity. this); <br/> View view = flater. inflate (R. layout. battery2, null); </P> <p> dialog. setcontentview (view, new viewgroup. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); </P> <p>/** <br/> * obtain the parent window displayed for the dialog, and set it when the dialog is displayed, other parts in the window are set to fuzzy <br/> * windowmanager. layoutparams. flag_blur_behind tells us that no matter what object is displayed in the front-end, <br/> * will be displayed in the top layer of the window <br/> */<br/> window = dialog. g Etwindow (); <br/> window. setflags (windowmanager. layoutparams. flag_blur_behind, windowmanager. layoutparams. flag_blur_behind); </P> <p>/** <br/> * pay attention to the findviewbyid () method. The main is loaded by default. components in XML. If you want to load X. if the ID in XML is <br/> * a component, add the setcontentview object to the front of the layout file, for example, the dialog <br/> */<br/> textview = (textview) dialog. findviewbyid (R. id. textview_message); <br/> textview. settext ("current cell phone power is" + level * 100/scale + "%"); </P> <p> button button2 = (button) dialog. findviewbyid (R. id. button_back); <br/> button2.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view V) {<br/> dialog. dismiss (); <br/> // cancel broadcast receiver <br/> unregisterreceiver (broadcastreceiver); </P> <p >}< br/> }); <br/> dialog. show (); </P> <p >}< br/> }; <br/> @ override <br/> Public void onclick (view v) {<br/> intentfilter filter = new intentfilter (action_name); <br/> registerreceiver (broadcastreceiver, filter); <br/>}</P> <p>}
Battery2.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" <br/> Android: orientation = "vertical"> <br/> <textview Android: text = "textview" Android: Id = "@ + ID/textview_message" <br/> Android: textsize = "10sp" <br/> Android: gravity = "center" <br/> Android: padding = "10px" <br/> Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> </textview> <br/> <button Android: Id = "@ + ID/button_back" Android: layout_width = "wrap_content" <br/> Android: TEXT = "return" Android: layout_height = "wrap_content"> </button> <br/> </linearlayout> <br/>
Battery. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" <br/> Android: orientation = "vertical"> <br/> <button Android: text = "" Android: Id = "@ + ID/button1" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> </linearlayout> <br/>