0

我正在尝试使用一些侦听器来控制我的应用程序的电池电量和电源连接,但由于某种原因,它们在应用程序开始时也没有得到更新。有什么方法可以手动完成吗?

类电话状态服务

private BroadcastReceiver mPowerConnectedReceiver;

private BroadcastReceiver mPowerDisconnectedReceiver;

private BroadcastReceiver mNormalPowerReceiver;

private BroadcastReceiver mLowPowerReceiver;

private static boolean sIsBatteryCharging = false;

private static boolean sIsBatteryLevelOk = true;

onCreate()

    mPowerConnectedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            PhoneStatusService.this.setChargingStatus(true);
        }
    };
    mPowerDisconnectedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            PhoneStatusService.this.setChargingStatus(false);
        }
    };
    mNormalPowerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            PhoneStatusService.this.setBatteryLevel(true);
        }
    };
    mLowPowerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            PhoneStatusService.this.setBatteryLevel(false);
        }
    };
    this.registerReceiver(mPowerConnectedReceiver, new IntentFilter(
            Intent.ACTION_POWER_CONNECTED));
    this.registerReceiver(mPowerDisconnectedReceiver, new IntentFilter(
            Intent.ACTION_POWER_DISCONNECTED));
    this.registerReceiver(mNormalPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_OKAY));
    this.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
4

0 回答 0