Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取Android中的电池电量和状态_Java_Android_Battery - Fatal编程技术网

Java 获取Android中的电池电量和状态

Java 获取Android中的电池电量和状态,java,android,battery,Java,Android,Battery,如何获取电池电量和状态(插入、放电、充电等)?我研究了开发人员文档,发现了一个BatterManager类。但它不包含任何方法,只包含常量。如何使用它?是一个代码示例,解释了如何获取电池信息 综上所述,动态设置了ACTION\u BATTERY\u CHANGEDintent的广播接收器,因为只有通过Context.registerReceiver()显式注册才能通过清单中声明的组件接收 你可以用这个来获得剩余的百分比费用 private void batteryLevel() {

如何获取电池电量和状态(插入、放电、充电等)?我研究了开发人员文档,发现了一个BatterManager类。但它不包含任何方法,只包含常量。如何使用它?

是一个代码示例,解释了如何获取电池信息

综上所述,动态设置了
ACTION\u BATTERY\u CHANGED
intent的广播接收器,因为只有通过
Context.registerReceiver()显式注册才能通过清单中声明的组件接收


你可以用这个来获得剩余的百分比费用

private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }

由于
sdk21
LOLLIPOP
可以使用以下方法获取当前电池电量的百分比:

BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

阅读

您不必注册实际的广播接收器,因为Android的BatteryManager使用的是粘性目的:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale;

return (int)(batteryPct*100);
这是来自于的官方文档。

基于,您可以在助手或Util类中使用此方法来获取当前电池百分比:

BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
Java版本:

public static int getBatteryPercentage(Context context) {

    if (Build.VERSION.SDK_INT >= 21) {

         BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
         return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

    } else {

         IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
         Intent batteryStatus = context.registerReceiver(null, iFilter);

         int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
         int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;

         double batteryPct = level / (double) scale;

         return (int) (batteryPct * 100);
   }
}
fun getBatteryPercentage(context: Context): Int {
    return if (Build.VERSION.SDK_INT >= 21) {
        val bm = context.getSystemService(BATTERY_SERVICE) as BatteryManager
        bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
    
    } else {
        val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        val batteryStatus: Intent = context.registerReceiver(null, iFilter)
        val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
        val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        val batteryPct = level / scale.toDouble()
        (batteryPct * 100).toInt()
    }
}
Kotlin版本:

public static int getBatteryPercentage(Context context) {

    if (Build.VERSION.SDK_INT >= 21) {

         BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
         return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

    } else {

         IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
         Intent batteryStatus = context.registerReceiver(null, iFilter);

         int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
         int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;

         double batteryPct = level / (double) scale;

         return (int) (batteryPct * 100);
   }
}
fun getBatteryPercentage(context: Context): Int {
    return if (Build.VERSION.SDK_INT >= 21) {
        val bm = context.getSystemService(BATTERY_SERVICE) as BatteryManager
        bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
    
    } else {
        val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        val batteryStatus: Intent = context.registerReceiver(null, iFilter)
        val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
        val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        val batteryPct = level / scale.toDouble()
        (batteryPct * 100).toInt()
    }
}

尝试此功能无需许可证或任何接收器

void getBattery_percentage()
{
      IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
      int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
      int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
      float batteryPct = level / (float)scale;
      float p = batteryPct * 100;

      Log.d("Battery percentage",String.valueOf(Math.round(p)));
  }

官方文件中有一个错误,您必须使用双精度浮点。因为0.53F*100F=52F

int level = batteryStatus != null ? 
batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1; int scale = 
batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1; 
double batteryPct = (double) level / (double) scale; int percent = (int) (batteryPct * 100D);

要使用电池管理器检查电池百分比,以下方法将返回电池百分比

private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }
来源


其他的答案没有提到如何去做


关于Intent.ACTION\u BATTERY\u CHANGED:这是一个粘性广播,包含有关电池的充电状态、电量和其他信息。您无法通过清单中声明的组件接收此信息,只能通过向Context.registerReceiver()显式注册它。@classAndroid好的,是的,答案中明确说明了这一点。是的@SirDarius,只是让其他人知道它。我首先对你的答案投了赞成票,当我发现它时,我发表了这篇评论。:)别忘了在Pause或onDestroy上注销Receiver。无法使用新的按键安装电池管理器,为什么?先生?官方文档中有一个错误,您必须使用double而不是float int level=batteryStatus!=无效的batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL,-1):-1;int scale=电池状态!=无效的batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE,-1):-1;双电池CT=(双)电平/(双)刻度;整数百分比=(整数)(电池体积*100D)@user2212515,你介意根据官方文档编辑我的答案吗?@Ryan,我编辑我的答案是为了支持android v21和更高版本。请检查一下。没有状态,只有level不是0.53F->52F错误,因为将最终值转换为int(从而从中删除任何十进制0.x值),而不是使用Math.round()?嘿,我使用了这个,有时结果是89,有时是00.89,移动小数点经过进一步研究,我找到了BatteryManager.EXTRA_LEVEL。检查此方法可在此处获取电池百分比无状态,仅为LEVEL最后,有人回答了电池状态