获取手机电池的剩余电量
2016-06-07 15:24
267 查看
取得手机电池的剩余量
* Android API中的BroadcastReceiver(Android.content.BroadcastReceiver)
* 类有点像Button中的Listener,当Receiver被注册后,会在后台等待其他程序
* 的调用,程序将通过注册BroadcastReceiver时设置的IntentFilter来捕捉系统
* 发出的Intent.ACTION_BATTERY_CHANGED这个action,再以此取得手机电池的剩
* 余量。
系统获取电池方法以及根据电量改变指示灯的颜色,在BatteryService类中修改
final int level = mBatteryProps.batteryLevel;
final int status = mBatteryProps.batteryStatus;
final int batteryLowLedOn = 1000;
final int batteryLowLedOff = 3000;
if (level <= 20) {
try {
if (mLight != null)
mLightsService.close(mLight.getType());
} catch (Exception e) {
e.printStackTrace();
}
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
// Solid red when battery is charging
// mBatteryLight.setColor(mBatteryLowARGB);
mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryLowARGB);
} else {
// Flash red when battery is low and not charging
// mBatteryLight.setFlashing(mBatteryLowARGB,
// LightsService.LIGHT_FLASH_TIMED,
// batteryLowLedOn, batteryLowLedOff);
mLight = new com.android.internal.policy.Light(Light.LOW_BATTERY, batteryLowLedOn,
batteryLowLedOff, mBatteryLowARGB);
}
try {
mLightsService.open(mLight);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// mBatteryLight.turnOff();
try {
if (mLight != null)
mLightsService.close(mLight.getType());
else
mLightsService.close(com.android.internal.policy.Light.POWER_ALL); //i dont have time to test,set this to avoid the light not off
} catch (Exception e) {
e.printStackTrace();
}
if (level < 90) {
mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryMediumARGB);
}else if(level >= 90){
mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE,3000, 5000,mBatteryFullARGB);
}else if (status == BatteryManager.BATTERY_STATUS_FULL){
// mBatteryLight.setColor(mBatteryFullARGB);
mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE, 0, 0, mBatteryFullARGB);
}
try {
mLightsService.open(mLight);
} catch (Exception e) {
e.printStackTrace();
}
}
* Android API中的BroadcastReceiver(Android.content.BroadcastReceiver)
* 类有点像Button中的Listener,当Receiver被注册后,会在后台等待其他程序
* 的调用,程序将通过注册BroadcastReceiver时设置的IntentFilter来捕捉系统
* 发出的Intent.ACTION_BATTERY_CHANGED这个action,再以此取得手机电池的剩
* 余量。
public class Ex06_02Activity extends Activity { private int intLevel; private int intScale; private Button mButton01; private AlertDialog d; // 创建BroadcastReceiver private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 如果捕捉到action是ACRION_BATTERY_CHANGED // 就运行onBatteryInfoReveiver() if (intent.ACTION_BATTERY_CHANGED.equals(action)) { intLevel = intent.getIntExtra("level", 0); intScale = intent.getIntExtra("scale", 100); onBatteryInfoReceiver(intLevel, intScale); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton01 = (Button) findViewById(R.id.myButton1); mButton01.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 注册一个BroadcastReceiver,作为访问电池计量之用 registerReceiver(mBatInfoReveiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } }); } // 拦截到ACTION_BATTRY_CHANGED后要执行的动作 private void onBatteryInfoReceiver(int intLevel, int intScale) { // TODO Auto-generated method stub d = new AlertDialog.Builder(Ex06_02Activity.this).create(); d.setTitle(R.string.str_dialog_title); d.setMessage(getResources().getString(R.string.str_dialog_body) + String.valueOf(intLevel * 100 / intScale) + "%"); d.setButton(getResources().getString(R.string.str_button2), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // 取消注册,并关闭对话框 unregisterReceiver(mBatInfoReveiver); d.dismiss(); } }); d.show(); }; }
系统获取电池方法以及根据电量改变指示灯的颜色,在BatteryService类中修改
final int level = mBatteryProps.batteryLevel;
final int status = mBatteryProps.batteryStatus;
final int batteryLowLedOn = 1000;
final int batteryLowLedOff = 3000;
if (level <= 20) {
try {
if (mLight != null)
mLightsService.close(mLight.getType());
} catch (Exception e) {
e.printStackTrace();
}
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
// Solid red when battery is charging
// mBatteryLight.setColor(mBatteryLowARGB);
mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryLowARGB);
} else {
// Flash red when battery is low and not charging
// mBatteryLight.setFlashing(mBatteryLowARGB,
// LightsService.LIGHT_FLASH_TIMED,
// batteryLowLedOn, batteryLowLedOff);
mLight = new com.android.internal.policy.Light(Light.LOW_BATTERY, batteryLowLedOn,
batteryLowLedOff, mBatteryLowARGB);
}
try {
mLightsService.open(mLight);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// mBatteryLight.turnOff();
try {
if (mLight != null)
mLightsService.close(mLight.getType());
else
mLightsService.close(com.android.internal.policy.Light.POWER_ALL); //i dont have time to test,set this to avoid the light not off
} catch (Exception e) {
e.printStackTrace();
}
if (level < 90) {
mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryMediumARGB);
}else if(level >= 90){
mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE,3000, 5000,mBatteryFullARGB);
}else if (status == BatteryManager.BATTERY_STATUS_FULL){
// mBatteryLight.setColor(mBatteryFullARGB);
mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE, 0, 0, mBatteryFullARGB);
}
try {
mLightsService.open(mLight);
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- Delete Node in a Linked List
- SwipeRefreshLayout+ListView实现下拉刷新自定义上拉加载
- 最小生成树 2016.7.20
- 汉字 字符的区别
- 如何获取手机上所有的音乐文件
- springMVC 的两种文件上传方式
- POST 传参
- 实体框架Entity Framework(EF) 官方资料入口
- 比较牛逼的答题卡扫描算法
- SESSION冲突
- 图像配准简介
- Android-XmlPullParser解析XML
- 查看CPU等参数配置
- 【官小飞】博主文章转载-文件上传和下载
- ios9和ios10的新特性
- AngularJS 实现的输入自动完成补充功能
- 调用外部js方法
- 网络服务器搭建的那些事(PV QPS Throughput)
- iOS app 后台运行利用本地推送处理蓝牙设备通讯指令
- 一些关于机器视觉的概念(转)