现在的位置: 首页 > 综合 > 正文

不注册广播,获得电量信息,不见得准确~

2013年08月18日 ⁄ 综合 ⁄ 共 613字 ⁄ 字号 评论关闭

android平台,获得电量信息需要注册一个广播,但是如何不注册广播获得呢?

见代码:

private IntentFilter mIntentFilter = new IntentFilter(
			Intent.ACTION_BATTERY_CHANGED);
Intent intent = registerReceiver(null, mIntentFilter);
		if (intent != null) {
			int level = intent.getIntExtra(
					android.os.BatteryManager.EXTRA_LEVEL, 0);
			boolean isPluggged = intent.getIntExtra(
					android.os.BatteryManager.EXTRA_PLUGGED, -1) > 0;
}

具体原因请自行参考API解释:

The system may broadcast Intents that are "sticky" -- these stay around after the broadcast as finished, to be sent to any later registrations. If your IntentFilter matches one of these sticky Intents, that Intent will be returned by this function
and sent to your receiver as if it had just been broadcast. 

抱歉!评论已关闭.