城市壁纸,女生与母亲合写论文,湖山盟
本文实例讲述了android查看电池电量的方法。分享给大家供大家参考,具体如下:
程序如下:
import android.app.activity; import android.app.dialog; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.graphics.color; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.button; import android.widget.textview; public class a02activity extends activity { private int level; private int scale; private button b01; private broadcastreceiver mbatinforeceiver=new broadcastreceiver(){ @override public void onreceive(context context, intent intent) { // todo auto-generated method stub string action=intent.getaction(); //如果捕捉到的是action_battery_changed就运行onbatteryinforeceiver();将电量显示于新窗口中 if(intent.action_battery_changed.equals(action)){ level=intent.getintextra("level", 0); scale=intent.getintextra("scale", 100); onbatteryinforeceiver(level,scale); } } }; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); b01=(button)findviewbyid(r.id.button01); b01.setbackgroundcolor(color.green); b01.settext("查看电量"); b01.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub registerreceiver(mbatinforeceiver,new intentfilter(intent.action_battery_changed)); } }); } public void onbatteryinforeceiver(int intlevel,int intscale){ final dialog d=new dialog(a02activity.this); d.settitle(r.string.str_title); d.setcontentview(r.layout.dialog); window window=d.getwindow(); window.setflags(windowmanager.layoutparams.flag_blur_behind, windowmanager.layoutparams.flag_blur_behind); textview mtextview02=(textview)d.findviewbyid(r.id.mytextview02); //取得电池电量显示于dialog中 mtextview02.settext(getresources().gettext(r.string.str_body)+string.valueof(intlevel*100/intscale)+"%"); button b02=(button)d.findviewbyid(r.id.button02); b02.setbackgroundcolor(color.red); b02.settext("返回"); b02.settextcolor(color.yellow); b02.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // 反注册receiver并关闭窗口 unregisterreceiver(mbatinforeceiver); d.dismiss(); } }); d.show(); } }
在android中,android.intent.battery_changed是系统的broadcast action message,当电池处于充电状态或电池电量有变化时,系统便会广播此action;程序中的broadcastreceiver在注册时,由于设置了intent filter过滤此action信息,因此当broadcastreceiver一被注册,就能马上捕捉这个action,进而取得电池电量。
主程序中的onreceiver()是当broadcastreceiver被触发时会运行的方法,写法如下:
public void onreceiver(context context,intent intent){ string action=intent.getaction(); if(intent.action_battery_changed.equals(action)){ /*运行程序的代码*/ } }
添加这一判断intent.action_battery_changed.equals(action)是为了确保broadcastreceiver只会被intent.action_battery_changed这个触发。如果没有这个判断程序也是可以运行的。
android api中说明,要注册含有intent.action_battery_changed的receiver,只能在程序中以context.registerreceiver()方式来注册,不能直接在androidmanifest.xml中注册。
本例中使用了让dialog在弹出时,背景的窗口呈现模糊的状态:
final dialog d=new dialog(a02activity.this); d.settitle(r.string.str_title); d.setcontentview(r.layout.dialog); window window=d.getwindow(); window.setflags(windowmanager.layoutparams.flag_blur_behind, windowmanager.layoutparams.flag_blur_behind);
其中windowmanager.layoutparams.flag_blur_behind是告诉目前的window不管是什么对象显示于前端,都会出现在window的最上层,让背景window呈现模糊状态。也可以在其他程序中使用这个效果。
更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》及《android开发入门与进阶教程》
希望本文所述对大家android程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论