美文网首页
Android11 增加低电量通知

Android11 增加低电量通知

作者: 梧叶已秋声 | 来源:发表于2021-01-29 11:03 被阅读0次

修改shouldSendBatteryLowLocked判断条件。

//frameworks\base\services\core\java\com\android\server\BatteryService.java
    private boolean shouldSendBatteryLowLocked() {
        final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
        final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;

        /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
         * - is just un-plugged (previously was plugged) and battery level is
         *   less than or equal to WARNING, or
         * - is not plugged and battery level falls to WARNING boundary
         *   (becomes <= mLowBatteryWarningLevel).
         */
        return !plugged
                && mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
                && mHealthInfo.batteryLevel <= mLowBatteryWarningLevel
                && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
    }

修改为

    private boolean shouldSendBatteryLowLocked() {
        final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
        final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;

        /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
         * - is just un-plugged (previously was plugged) and battery level is
         *   less than or equal to WARNING, or
         * - is not plugged and battery level falls to WARNING boundary
         *   (becomes <= mLowBatteryWarningLevel).
         */
        return !plugged
                && mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
                && mHealthInfo.batteryLevel <= mLowBatteryWarningLevel;
              //  && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
    }

修改低电量通知的值,默认为15,先需修改为10,即电量低于百分之十的时候,出现低电量通知。

//frameworks\base\core\res\res\values\config.xml
    <integer name="config_lowBatteryWarningLevel">10</integer>

增加filter.addAction(Intent.ACTION_BATTERY_LOW);

else if (Intent.ACTION_BATTERY_LOW.equals(action)){
                mCurrentBatteryStateSnapshot = new BatteryStateSnapshot(mBatteryLevel, mPowerManager.isPowerSaveMode(),
                        false, findBatteryLevelBucket(mBatteryLevel), mBatteryStatus, mLowBatteryReminderLevels[1],
                        mLowBatteryReminderLevels[0]);
                mWarnings.updateSnapshot(mCurrentBatteryStateSnapshot);
                mWarnings.showLowBatteryWarning(true);
            }

具体位置如下。

//vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\power\PowerUI.java
   @VisibleForTesting
    final class Receiver extends BroadcastReceiver {

        private boolean mHasReceivedBattery = false;

        public void init() {
            // Register for Intent broadcasts for...
            IntentFilter filter = new IntentFilter();
            filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);

            filter.addAction(Intent.ACTION_BATTERY_LOW);

            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
            ..............
        }


        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) {
                ThreadUtils.postOnBackgroundThread(() -> {
                    if (mPowerManager.isPowerSaveMode()) {
                        mWarnings.dismissLowBatteryWarning();
                    }
                });
            }else if (Intent.ACTION_BATTERY_LOW.equals(action)){
                mCurrentBatteryStateSnapshot = new BatteryStateSnapshot(mBatteryLevel, mPowerManager.isPowerSaveMode(),
                        false, findBatteryLevelBucket(mBatteryLevel), mBatteryStatus, mLowBatteryReminderLevels[1],
                        mLowBatteryReminderLevels[0]);
                mWarnings.updateSnapshot(mCurrentBatteryStateSnapshot);
                mWarnings.showLowBatteryWarning(true);
            } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
            ..............
    }


}

参考链接:
android开发笔记之电池低电处理
Android10 低电量通知
电量提醒
Android Low Battery 低电量处理流程

相关文章

网友评论

      本文标题:Android11 增加低电量通知

      本文链接:https://www.haomeiwen.com/subject/mdfqtltx.html