当前位置:网站首页>andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
2022-07-28 22:46:00 【android framework】
之前android5.0一篇博客分析过了android低功耗模式,这边我们需要在低功耗模式下新增一些功能,就直接分析代码:
一、新增功能函数位置
在低功耗的最后生效的函数中,增加一个函数,执行我们需要的功能:
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)//发广播
.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);//各个回调
}
intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mContext.sendBroadcast(intent);
handlePowerSaveMode(mLowPowerModeEnabled);//新增功能函数
}
});
}
}
二、新增函数
一些初始化在systemReady函数中:
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);//数据流量
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);//控制震动设置
BluetoothManager bluetoothManager = (BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE);//蓝牙
mBluetoothAdapter = bluetoothManager.getAdapter();
handlePowerSaveMode函数就是我们主要的功能实现,如下:
private void handlePowerSaveMode(boolean LowPowerMode) {
final ContentResolver resolver = mContext.getContentResolver();
if (LowPowerMode) {//低功耗模式
mNoLowPowerScreenoffTimeout = Settings.System.getIntForUser(resolver,
Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT,
UserHandle.USER_CURRENT);//获取之前的屏幕睡眠时间
mNoLowPowerScreenBrightness = Settings.System.getIntForUser(resolver,
Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessSettingDefault,
UserHandle.USER_CURRENT);//获取之前的屏幕亮度
mNoLowPowerLocationMode = Settings.Secure.getIntForUser(resolver,
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF,
UserHandle.USER_CURRENT);//获取之前定位状态
mNoLowPowerWifiApState = mWifiManager.getWifiApState();//wifi热点状态
mNoLowPowerWifiState = mWifiManager.getWifiState();//wifi状态
mNoLowPowerDataEnabled = mTelephonyManager.getDataEnabled();//数据流量状态
mNoLowPowerBluetoothEnabled = mBluetoothAdapter.isEnabled();//蓝牙状态
mNoLowPowerVibrateRingerSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);//铃声震动
mNoLowPowerVibrateNotificationSetting= mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION);//通知震动
Settings.System.putInt(resolver, Settings.System.SCREEN_OFF_TIMEOUT, 15000);//设置屏幕睡眠时间15秒
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, 10);//设置屏幕亮度为10
Settings.Secure.putInt(resolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);//关闭定位
mWifiManager.setWifiApEnabled(null, false);//关闭wifi热点
mWifiManager.setWifiEnabled(false);//关闭wifi
mTelephonyManager.setDataEnabled(false);//关闭数据流量
mBluetoothAdapter.disable();//关闭蓝牙
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);//关闭铃声震动
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);//关闭通知震动
} else {//这边是低功耗模式关闭后,一系列的恢复动作
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();
}
}
}
这里好多都使用了Settings里面的数据库,是因为好多设置都是监听这个数据库,比如PowermanagerService会监听亮度,睡眠时间。而settings应用进入也是根据这个settings的数据库设置选项的
————————————————
版权声明:本文为CSDN博主「kc专栏」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kc58236582/article/details/50843954
边栏推荐
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- Recursion / backtracking (Part 2)
- Erc20 Standard Code
- 16. Influence of deviation, variance, regularization and learning curve on the model
- Anti shake and throttling
- Attack and defense world web master advanced area PHP_ rce
- Recursion / backtracking (middle)
- @Detailed explanation of postconstruct annotation
- 动态规划问题(七)
- I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!
猜你喜欢

PTA (daily question) 7-75 how many people in a school

动态规划问题(五)

【开发教程10】疯壳·开源蓝牙心率防水运动手环-蓝牙 BLE 收发

Introduction and solution of common security vulnerabilities in Web System SQL injection

时间序列统计分析

“吃货联盟定餐系统”

vulnhub:SolidState

vulnhub:BTRSys2

Advanced area of attack and defense world web masters unserialize3

Brief introduction to compressed sensing
随机推荐
[untitled]
递归/回溯刷题(中)
How to solve the problems of MQ message loss, duplication and backlog?
NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
MySQL stored procedure
Camera Hal OEM module ---- CMR_ preview.c
Install mysql5.7 under Linux, super detailed complete tutorial, and cloud MySQL connection
动态规划问题(四)
Application and principle of distributed current limiting redistribution rratelimiter
The 30th day of question brushing
Installation and use of pnpm
CDN mode uses vant components, and components cannot be called after they are introduced
Dynamic programming problem (VII)
Laravel8 middleware realizes simple permission control
最长上升子序列
Dynamic programming problem (VIII)
PTA (daily question) 7-74 yesterday
vscode下链接远程服务器安装插件失败、速度慢等解决方法
Html+css+php+mysql realize registration + login + change password (with complete code)
CV target detection model sketch (2)