在android中,要获得手机的电量的话,可以通过intent广播去实现,监听
action_battery_changed,例子如下:
-
packagecom.liao;
-
importandroid.app.Activity;
-
importandroid.content.BroadcastReceiver;
-
importandroid.content.Context;
-
importandroid.content.Intent;
-
importandroid.content.IntentFilter;
-
importandroid.os.Bundle;
-
importandroid.widget.TextView;
-
-
publicclassBatteryLevelActivityextendsActivity{
-
-
privateTextViewbatterLevel;
-
-
@Override
-
-
-
-
publicvoidonCreate(Bundleicicle){
-
super.onCreate(icicle);
-
setContentView(R.layout.main);
-
batterLevel=(TextView)this.findViewById(R.id.batteryLevel);
-
batteryLevel();
-
}
-
-
privatevoidbatteryLevel(){
-
BroadcastReceiverbatteryLevelReceiver=newBroadcastReceiver(){
-
publicvoidonReceive(Contextcontext,Intentintent){
-
context.unregisterReceiver(this);
-
intrawlevel=intent.getIntExtra("level",-1);
-
intscale=intent.getIntExtra("scale",-1);
-
-
intlevel=-1;
-
if(rawlevel>=0&&scale>0){
-
level=(rawlevel*100)/scale;
-
}
-
batterLevel.setText("BatteryLevelRemaining:"+level+"%");
-
}
-
};
-
IntentFilterbatteryLevelFilter=newIntentFilter(Intent.ACTION_BATTERY_CHANGED);
-
registerReceiver(batteryLevelReceiver,batteryLevelFilter);
-
}
-
-
}
如果是2。0后的SDK,也可以这样:
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
就是修改一下类而已
分享到:
相关推荐
android获取手机电量
主要介绍了Android获取手机电池电量用法,以完整实例形式较为详细的分析了Android获取手机电量的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
【Android】获取电池电量 Demo
Android 获取手机当前电量, int level;定义的是当前电池电量,int scale;定义的是手机总电量。注册访问电池剩余量的系统BroadcastReceiver,当前手机电量为:" level*100/scale "%,这个计算方法相信大家都会吧。
一个安卓获取手机电量使用明细的小例子源码,手机中各个应用的耗电信息,真机通过文件计算,虚拟机通过计算CUP利用时间显示每个程序对电量的使用百分比。
获取电池电量的多少,或者电池其它一些信息。
实现一个电池属性实时监听器。这些属性包括温度,电池电量....等等。而且,使用Service,Broadcast,以及Thread的方式来实现。MyBatteryService后台获取Battery属性值,然后,在Activity中进行相关数据的显示。
Android-有没有什么方法能够快速、实时的获取android手机的电量值? - 德问编程社交问答
获取android手机的电量WiFi运营商的信息,目前只有这些功能,其他的暂时没想到
简单的android取电量源码,直接打开工程,可自己在原有基础上添加修改。
这个应用很简单就是获取Android中手机中应用用电排行的,很方便实用的
App电量计算统计和硬件耗电量计算,代码里面详细介绍
利用广播获取手机 实时电量信息
实验十一 通过系统广播获取手机电量的变化
这个例子主要是获取Android系统手机的电量信息,使用了intent.getExtras()方法来获取实现,关于此方法的使用,请参考以下的代码: public void onReceive(Context context, Intent intent) {//重写onReceiver方法 ...
手机电池电量的获取在应用程序的开发中是很常用的,通过这个案例来说明如何在代码中获取手机电池的电量。
一个安卓获取手机电量使用明细的小例子源码,手机中各个应用的耗电信息,真机通过文件计算,虚拟机通过计算CUP利用时间显示每个程序对电量的使用百分比。
接收系统广播实现查看电池电量,这里用到了 Intent.ACTION_BATTERY_CHANGED 事件
有自己简单封装的一个接口。文件中也有官方的例子,可以自己下来改改。 相关博客为:http://blog.csdn.net/fansongy/article/details/8927734