失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 获取手机电池的剩余电量

获取手机电池的剩余电量

时间:2019-10-17 08:21:05

相关推荐

获取手机电池的剩余电量

取得手机电池的剩余量

* 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());elsemLightsService.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();}}

如果觉得《获取手机电池的剩余电量》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。