无法获取充电状态

Can't get charging state

我正在尝试从电池获取信息。所以我创建了 BroadcastReceiver,在后台读取这个值。我遇到了从电池获取 isCharging 状态的问题。我总是假的。我正在尝试使用此代码:

public class AlertReceiver extends BroadcastReceiver {


@Overrid
public void onReceive(Context context, Intent intent) {

 
    BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
    assert bm != null;
    int battery_level = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

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

    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;
  }
}

清单

 <receiver android:name=".service.AlertReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
            <action android:name="BackgroundProcess"/>
        </intent-filter>
    </receiver>

使用我们的 BroadcastReceiver 实现创建一个内部 class:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context ctxt, Intent intent) {
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                txvlebtry.setText(String.valueOf(level)+"%" );
                int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
    
                if (health == BatteryManager.BATTERY_HEALTH_COLD) {
                    btry_health_val.setText(R.string.cold);
                }
                if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
                    btry_health_val.setText(R.string.dead);
                }
                if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
                    btry_health_val.setText(R.string.good);
                }
                if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
                    btry_health_val.setText(R.string.overheat);
                }
                if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                    btry_health_val.setText(R.string.overVoltage);
                }
                if (health == BatteryManager.BATTERY_HEALTH_UNKNOWN) {
                    btry_health_val.setText(R.string.unknown);
                }
    
                int temprature = (intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;
                tempraturebtry_val.setText(temprature + R.string.degreecel);
                int chargeplug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
    
                if (chargeplug == BatteryManager.BATTERY_PLUGGED_AC) {
                    chargeplug_val.setText(R.string.acadapter);
                }
                if (chargeplug == BatteryManager.BATTERY_PLUGGED_USB) {
                    chargeplug_val.setText(R.string.USB);
                }
                if (chargeplug == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
                    chargeplug_val.setText(R.string.Wireless);
                }
    
                int status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
    
                if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    btrystatus_val.setText(R.string.Charging);
                }
                if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                    btrystatus_val.setText(R.string.Discharging);
                }
                if (status == BatteryManager.BATTERY_STATUS_FULL) {
                    btrystatus_val.setText(R.string.Full);
                }
                if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                    btrystatus_val.setText(R.string.notchargin);
                }
                if (status == BatteryManager.BATTERY_STATUS_UNKNOWN) {
                    btrystatus_val.setText(R.string.unknown);
                }
    
            }
        };

然后在application中注册我们的BroadcastReceiver

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
.....

  this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

}