在代码中而不是在
AndroidManifest文件中注册您的接收器。
registerReceiver(batteryChangeReceiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); // register in activity or servicepublic class BatteryChangeReceiver extends BroadcastReceiver { int scale = -1; int level = -1; int voltage = -1; int temp = -1; @Override public void onReceive(Context context, Intent intent) { level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); }}unregisterReceiver(batteryChangeReceiver);//unregister in the activity or service
或使用
null接收器收听电池电量。
Intent BATTERYintent = this.registerReceiver(null, new IntentFilter( Intent.ACTION_BATTERY_CHANGED));int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);Log.v(null, "LEVEL" + level);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)