获取手机电量

	private IntentFilter mFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

	public static final int MSG_BATT = 100;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == MSG_BATT) {

				Intent intent = (Intent) msg.obj;
				int rawlevel = intent.getIntExtra("level", -1);// 获得当前电量
				int scale = intent.getIntExtra("scale", -1);//获得总电量

				System.out.println();
			}
		}

	};

	/** 检查电量 */
	public void checkPower(Context context) {

		context.registerReceiver(new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				context.unregisterReceiver(this);
				handler.removeMessages(MSG_BATT);
				handler.obtainMessage(MSG_BATT, intent).sendToTarget();
			}
		}, mFilter);

		handler.sendEmptyMessageDelayed(MSG_BATT, 1000);

	}

你可能感兴趣的:(获取手机电量)