如何检测何时电池电量低:Android?

如何检测何时电池电量低:Android?,第1张

如何检测何时电池电量低:Android?

在代码中而不是在

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);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5490039.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存