Android - обратная зарядка и разрядка аккумулятора в Red Mi Note 4

Я пишу код для выполнения некоторых действий во время зарядки аккумулятора, но в телефоне Xioami я получаю обратный ответ от приемника. Означает, что когда питание подключено, оно реагирует на состояние разряда, а когда питание отключено, оно отвечает на состояние зарядки. Вот мой код, пожалуйста, руководство.

Манифест, как показано ниже:

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".PowerConnectionReceiver">
            <intent-filter android:priority="100">
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />

            </intent-filter>
        </receiver>
    </application>

Код получателя:

public class PowerConnectionReceiver extends BroadcastReceiver {



    public void onReceive(Context context, Intent intent) {

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


        // Are we charging / charged?
        assert batteryStatus != null;
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
            Toast.makeText(context, "Power Started", Toast.LENGTH_SHORT).show();
        } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
            Toast.makeText(context, "Power Stopped", Toast.LENGTH_SHORT).show();
        }


        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;     
        boolean isnotchrg = status == BatteryManager.BATTERY_STATUS_NOT_CHARGING;
        boolean isdisch = status == BatteryManager.BATTERY_STATUS_DISCHARGING;


        if (isCharging) {
            Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
        }
        if (isnotchrg) {
            Toast.makeText(context, "Not Charging", Toast.LENGTH_SHORT).show();
        }
        if (isdisch) {
            Toast.makeText(context, "Discharging", Toast.LENGTH_SHORT).show();
        }
}

Состояние подключенного питания работает правильно, Состояние аккумулятора работает обратно. Код работает нормально во многих других телефонах и мипад тоже

0 ответов

Другие вопросы по тегам