当前位置:网站首页>Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
2022-07-29 00:42:00 【android framework】
Before android5.0 A blog has analyzed android Low power mode , Here we need to add some functions in the low-power mode , Just analyze the code directly :
One 、 New function location
In the last effective function with low power consumption , Add a function , Perform the functions we need :
void updateLowPowerModeLocked() {
if (mIsPowered && mLowPowerModeSetting) {
if (DEBUG_SPEW) {
Slog.d(TAG, "updateLowPowerModeLocked: powered, turning setting off");
}
// Turn setting off if powered
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE, 0);
mLowPowerModeSetting = false;
}
final boolean autoLowPowerModeEnabled = !mIsPowered && mAutoLowPowerModeConfigured
&& !mAutoLowPowerModeSnoozing && mBatteryLevelLow;
final boolean lowPowerModeEnabled = mLowPowerModeSetting || autoLowPowerModeEnabled;
if (mLowPowerModeEnabled != lowPowerModeEnabled) {
mLowPowerModeEnabled = lowPowerModeEnabled;
powerHintInternal(POWER_HINT_LOW_POWER, lowPowerModeEnabled ? 1 : 0);
BackgroundThread.getHandler().post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING)// Broadcast
.putExtra(PowerManager.EXTRA_POWER_SAVE_MODE, mLowPowerModeEnabled)
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mContext.sendBroadcast(intent);
ArrayList<PowerManagerInternal.LowPowerModeListener> listeners;
synchronized (mLock) {
listeners = new ArrayList<PowerManagerInternal.LowPowerModeListener>(
mLowPowerModeListeners);
}
for (int i=0; i<listeners.size(); i++) {
listeners.get(i).onLowPowerModeChanged(lowPowerModeEnabled);// Each callback
}
intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mContext.sendBroadcast(intent);
handlePowerSaveMode(mLowPowerModeEnabled);// New function
}
});
}
}
Two 、 New function
Some initializations are in systemReady Function :
public void systemReady(IAppOpsService appOps) {
synchronized (mLock) {
mSystemReady = true;
mAppOps = appOps;
mDreamManager = getLocalService(DreamManagerInternal.class);
mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class);
mPolicy = getLocalService(WindowManagerPolicy.class);
mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);//wifi
mTelephonyManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);// Data traffic
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);// Control vibration settings
BluetoothManager bluetoothManager = (BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE);// bluetooth
mBluetoothAdapter = bluetoothManager.getAdapter();
handlePowerSaveMode Function is our main function implementation , as follows :
private void handlePowerSaveMode(boolean LowPowerMode) {
final ContentResolver resolver = mContext.getContentResolver();
if (LowPowerMode) {// Low power mode
mNoLowPowerScreenoffTimeout = Settings.System.getIntForUser(resolver,
Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT,
UserHandle.USER_CURRENT);// Get the previous screen sleep time
mNoLowPowerScreenBrightness = Settings.System.getIntForUser(resolver,
Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessSettingDefault,
UserHandle.USER_CURRENT);// Get the previous screen brightness
mNoLowPowerLocationMode = Settings.Secure.getIntForUser(resolver,
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF,
UserHandle.USER_CURRENT);// Get the previous location status
mNoLowPowerWifiApState = mWifiManager.getWifiApState();//wifi Hot spot status
mNoLowPowerWifiState = mWifiManager.getWifiState();//wifi state
mNoLowPowerDataEnabled = mTelephonyManager.getDataEnabled();// Data flow status
mNoLowPowerBluetoothEnabled = mBluetoothAdapter.isEnabled();// Bluetooth status
mNoLowPowerVibrateRingerSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);// Ringing vibration
mNoLowPowerVibrateNotificationSetting= mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION);// Notice vibration
Settings.System.putInt(resolver, Settings.System.SCREEN_OFF_TIMEOUT, 15000);// Set the screen sleep time 15 second
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, 10);// Set the screen brightness to 10
Settings.Secure.putInt(resolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);// Turn off positioning
mWifiManager.setWifiApEnabled(null, false);// close wifi hotspot
mWifiManager.setWifiEnabled(false);// close wifi
mTelephonyManager.setDataEnabled(false);// Turn off data traffic
mBluetoothAdapter.disable();// Turn off Bluetooth
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);// Turn off ringing vibration
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);// Turn off notification vibration
} else {// Here is after the low power consumption mode is turned off , A series of recovery actions
Settings.System.putInt(resolver, Settings.System.SCREEN_OFF_TIMEOUT, mNoLowPowerScreenoffTimeout);
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, mNoLowPowerScreenBrightness);
Settings.Secure.putInt(resolver, Settings.Secure.LOCATION_MODE, mNoLowPowerLocationMode);
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, mNoLowPowerVibrateRingerSetting);
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, mNoLowPowerVibrateNotificationSetting);
if (mNoLowPowerWifiApState == WifiManager.WIFI_AP_STATE_ENABLED) {
mWifiManager.setWifiApEnabled(null, true);
}
if (mNoLowPowerWifiState == WifiManager.WIFI_STATE_ENABLED) {
mWifiManager.setWifiEnabled(true);
}
mTelephonyManager.setDataEnabled(mNoLowPowerDataEnabled);
if (mNoLowPowerBluetoothEnabled) {
mBluetoothAdapter.enable();
}
}
}
A lot of them are used here Settings The database inside , Because many settings are monitoring this database , such as PowermanagerService Will monitor the brightness , Sleep time . and settings Application entry is also based on this settings Database setting options
————————————————
Copyright notice : This paper is about CSDN Blogger 「kc special column 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/kc58236582/article/details/50843954
边栏推荐
- 15. Model evaluation and selection
- Attack and defense world web master advanced area php2
- 递归/回溯刷题(下)
- Why is it so difficult for the SEC to refuse the application for transferring gray-scale GBTC to spot ETF? What is the attraction of ETF transfer?
- Cause analysis of 12 MySQL slow queries
- Dynamic programming problem (4)
- MySQL sub database and sub table and its smooth expansion scheme
- Oracle实例无法启动的问题如何解决
- PTA (daily question) 7-72 calculate the cumulative sum
- 16.偏差、方差、正则化、学习曲线对模型的影响
猜你喜欢

MySQL sub database and sub table and its smooth expansion scheme

Use hutool tool class to operate excel with more empty Sheet1

"Food alliance ordering system"

NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作

Google browser, no installation required

PTA (daily question) 7-70 diamond

16.偏差、方差、正则化、学习曲线对模型的影响

Advanced area of attack and defense world web masters -baby Web

“吃货联盟定餐系统”

Dynamic programming problem (3)
随机推荐
Dynamic programming problem (6)
Introduction of shortest path tree (SPT) and matlab code
Redis learning notes
Dynamic programming problem (3)
16.偏差、方差、正则化、学习曲线对模型的影响
面试被问到了String相关的几道题,你能答上来吗?
Anti shake and throttling
MySQL transaction (this is enough...)
NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
[basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)
PTA (daily question) 7-73 turning triangle
How to solve Oracle not available
15. Model evaluation and selection
Laravel permission control
MySQL事务(transaction) (有这篇就足够了..)
flask结合容联云发送验证码
Cause analysis of 12 MySQL slow queries
[untitled]
总结:POD与容器的区别
Talk about seven ways to realize asynchronous programming