获取手机电池的剩余电量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31307919/article/details/51604027
取得手机电池的剩余量
 * 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();
					}
				}


没有更多推荐了,返回首页