当前位置:网站首页>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
边栏推荐
- 刷题的第三十天
- Table custom style row class name in elemenui
- Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
- 还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...
- MySQL的存储过程
- What does the expression > > 0 in JS mean
- 乱打日志的男孩运气怎么样我不知道,加班肯定很多!
- vulnhub:SolidState
- PTA (daily question) 7-72 calculate the cumulative sum
- MySQL transaction (this is enough...)
猜你喜欢

会议OA项目之会议通知&会议反馈&反馈详情功能

Application and principle of distributed current limiting redistribution rratelimiter

Dynamic programming (V)

Simple use and understanding of laravel message queue

Oracle实例无法启动的问题如何解决

IMG tags prohibit dragging pictures

MySQL stored procedure

Brief introduction to compressed sensing

17.机器学习系统的设计

Dynamic programming problem (6)
随机推荐
1331. Array sequence number conversion: simple simulation question
Flask sends verification code in combination with Ronglian cloud
Solutions such as failed plug-in installation and slow speed of linking remote server under vscode
Attack and defense world web master advanced area web_ php_ include
Dynamic programming (V)
MySQL 分库分表及其平滑扩容方案
Dynamic programming problem (1)
110 MySQL interview questions and answers (continuously updated)
flyway的快速入门教程
Talk about seven ways to realize asynchronous programming
乱打日志的男孩运气怎么样我不知道,加班肯定很多!
16.偏差、方差、正则化、学习曲线对模型的影响
Mock.js essay
【开发教程10】疯壳·开源蓝牙心率防水运动手环-蓝牙 BLE 收发
2022DASCTF7月赋能赛(复现)
Advanced area of attack and defense world web masters supersqli
手把手教你安装Latex(保姆级教程)
PTA (daily question) 7-77 encryption
vulnhub:BTRSys2
Table custom style row class name in elemenui