Android Settings 按住電源按鈕的操作方法
如題,Android 原生 Settings 里有個(gè) 按住電源按鈕 的選項(xiàng),可以設(shè)置按住電源按鈕的操作。

按住電源按鈕
兩個(gè)選項(xiàng)的 UI 是分離的,
電源菜單
代碼在 packages/apps/Settings/src/com/android/settings/gestures/LongPressPowerForPowerMenuPreferenceController.java ,
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.gestures;
import android.content.Context;
import android.net.Uri;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
/**
* Configures the behaviour of the radio selector to configure long press power button to Power
* Menu.
*/
public class LongPressPowerForPowerMenuPreferenceController extends BasePreferenceController
implements PowerMenuSettingsUtils.SettingsStateCallback,
SelectorWithWidgetPreference.OnClickListener,
LifecycleObserver {
private SelectorWithWidgetPreference mPreference;
private final PowerMenuSettingsUtils mUtils;
public LongPressPowerForPowerMenuPreferenceController(Context context, String key) {
super(context, key);
mUtils = new PowerMenuSettingsUtils(context);
}
@Override
public int getAvailabilityStatus() {
return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference != null) {
mPreference.setOnClickListener(this);
}
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (preference instanceof SelectorWithWidgetPreference) {
((SelectorWithWidgetPreference) preference)
.setChecked(
!PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));
}
}
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);
if (mPreference != null) {
updateState(mPreference);
}
}
@Override
public void onChange(Uri uri) {
if (mPreference != null) {
updateState(mPreference);
}
}
/** @OnLifecycleEvent(Lifecycle.Event.ON_START) */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mUtils.registerObserver(this);
}
/** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mUtils.unregisterObserver();
}
}關(guān)鍵代碼,
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);
if (mPreference != null) {
updateState(mPreference);
}
}數(shù)字助理
代碼在 packages/apps/Settings/src/com/android/settings/gestures/LongPressPowerForAssistantPreferenceController.java ,
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.gestures;
import android.content.Context;
import android.net.Uri;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
/**
* Configures the behaviour of the radio selector to configure long press power button to Assistant.
*/
public class LongPressPowerForAssistantPreferenceController extends BasePreferenceController
implements PowerMenuSettingsUtils.SettingsStateCallback,
SelectorWithWidgetPreference.OnClickListener,
LifecycleObserver {
private SelectorWithWidgetPreference mPreference;
private final PowerMenuSettingsUtils mUtils;
public LongPressPowerForAssistantPreferenceController(Context context, String key) {
super(context, key);
mUtils = new PowerMenuSettingsUtils(context);
}
@Override
public int getAvailabilityStatus() {
return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference != null) {
mPreference.setOnClickListener(this);
}
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (preference instanceof SelectorWithWidgetPreference) {
((SelectorWithWidgetPreference) preference)
.setChecked(
PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));
}
}
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
if (mPreference != null) {
updateState(mPreference);
}
}
@Override
public void onChange(Uri uri) {
if (mPreference != null) {
updateState(mPreference);
}
}
/** @OnLifecycleEvent(Lifecycle.Event.ON_START) */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mUtils.registerObserver(this);
}
/** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mUtils.unregisterObserver();
}
}關(guān)鍵代碼,
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
if (mPreference != null) {
updateState(mPreference);
}
}功能設(shè)置
實(shí)際設(shè)置是在 packages/apps/Settings/src/com/android/settings/gestures/PowerMenuSettingsUtils.java ,
private static final String POWER_BUTTON_LONG_PRESS_SETTING =
Settings.Global.POWER_BUTTON_LONG_PRESS;
private static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1; // a.k.a., Power Menu
private static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5; // Settings.Secure.ASSISTANT
// ...
public static boolean setLongPressPowerForAssistant(Context context) {
if (Settings.Global.putInt(
context.getContentResolver(),
POWER_BUTTON_LONG_PRESS_SETTING,
LONG_PRESS_POWER_ASSISTANT_VALUE)) {
// Make power + volume up buttons to open the power menu
Settings.Global.putInt(
context.getContentResolver(),
KEY_CHORD_POWER_VOLUME_UP_SETTING,
KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS);
return true;
}
return false;
}
public static boolean setLongPressPowerForPowerMenu(Context context) {
if (Settings.Global.putInt(
context.getContentResolver(),
POWER_BUTTON_LONG_PRESS_SETTING,
LONG_PRESS_POWER_GLOBAL_ACTIONS)) {
// We restore power + volume up buttons to the default action.
int keyChordDefaultValue =
context.getResources()
.getInteger(KEY_CHORD_POWER_VOLUME_UP_DEFAULT_VALUE_RESOURCE);
Settings.Global.putInt(
context.getContentResolver(),
KEY_CHORD_POWER_VOLUME_UP_SETTING,
keyChordDefaultValue);
return true;
}
return false;
}追蹤初始化邏輯
android.provider.Settings 相關(guān)調(diào)用的初始化一般都在 SettingsProvider 里,
找到 frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java ,
/**
* Correctly sets long press power button Behavior.
*
* The issue is that setting for LongPressPower button Behavior is not available on all devices
* and actually changes default Behavior of two properties - the long press power button
* and volume up + power button combo. OEM can also reconfigure these Behaviors in config.xml,
* so we need to be careful that we don't irreversibly change power button Behavior when
* restoring. Or switch to a non-default button Behavior.
*/
private void setLongPressPowerBehavior(ContentResolver cr, String value) {
// We will not restore the value if the long press power setting option is unavailable.
if (!mContext.getResources().getBoolean(
com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable)) {
return;
}
int longPressOnPowerBehavior;
try {
longPressOnPowerBehavior = Integer.parseInt(value);
} catch (NumberFormatException e) {
return;
}
if (longPressOnPowerBehavior < LONG_PRESS_POWER_NOTHING
|| longPressOnPowerBehavior > LONG_PRESS_POWER_FOR_ASSISTANT) {
return;
}
// When user enables long press power for Assistant, we also switch the meaning
// of Volume Up + Power key chord to the "Show power menu" option.
// If the user disables long press power for Assistant, we switch back to default OEM
// Behavior configured in config.xml. If the default Behavior IS "LPP for Assistant",
// then we fall back to "Long press for Power Menu" Behavior.
if (longPressOnPowerBehavior == LONG_PRESS_POWER_FOR_ASSISTANT) {
Settings.Global.putInt(cr, Settings.Global.POWER_BUTTON_LONG_PRESS,
LONG_PRESS_POWER_FOR_ASSISTANT);
Settings.Global.putInt(cr, Settings.Global.KEY_CHORD_POWER_VOLUME_UP,
KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS);
} else {
// We're restoring "LPP for Assistant Disabled" state, prefer OEM config.xml Behavior
// if possible.
int longPressOnPowerDeviceBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior);
if (longPressOnPowerDeviceBehavior == LONG_PRESS_POWER_FOR_ASSISTANT) {
// The default on device IS "LPP for Assistant Enabled" so fall back to power menu.
Settings.Global.putInt(cr, Settings.Global.POWER_BUTTON_LONG_PRESS,
LONG_PRESS_POWER_GLOBAL_ACTIONS);
} else {
// The default is non-Assistant Behavior, so restore that default.
Settings.Global.putInt(cr, Settings.Global.POWER_BUTTON_LONG_PRESS,
longPressOnPowerDeviceBehavior);
}
// Clear and restore default power + volume up Behavior as well.
int powerVolumeUpDefaultBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_keyChordPowerVolumeUp);
Settings.Global.putInt(cr, Settings.Global.KEY_CHORD_POWER_VOLUME_UP,
powerVolumeUpDefaultBehavior);
}
}找到 config_longPressOnPowerBehavior ,定義在 frameworks/base/core/res/res/values/config.xml ,
<!-- Control the behavior when the user long presses the power button.
0 - Nothing
1 - Global actions menu
2 - Power off (with confirmation)
3 - Power off (without confirmation)
4 - Go to voice assist
5 - Go to assistant (Settings.Secure.ASSISTANT)
-->
<integer name="config_longPressOnPowerBehavior">5</integer>按住電源按鈕的持續(xù)時(shí)間
代碼在 packages/apps/Settings/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceController.java ,
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.gestures;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.SliderPreferenceController;
import com.android.settings.widget.LabeledSeekBarPreference;
/** Handles changes to the long press power button sensitivity slider. */
public class LongPressPowerSensitivityPreferenceController extends SliderPreferenceController
implements PowerMenuSettingsUtils.SettingsStateCallback, LifecycleObserver {
@Nullable
private final int[] mSensitivityValues;
private final PowerMenuSettingsUtils mUtils;
@Nullable
private LabeledSeekBarPreference mPreference;
public LongPressPowerSensitivityPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mSensitivityValues = context.getResources().getIntArray(
com.android.internal.R.array.config_longPressOnPowerDurationSettings);
mUtils = new PowerMenuSettingsUtils(context);
}
/** @OnLifecycleEvent(Lifecycle.Event.ON_START) */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mUtils.registerObserver(this);
}
/** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mUtils.unregisterObserver();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference != null) {
mPreference.setContinuousUpdates(false);
mPreference.setHapticFeedbackMode(
LabeledSeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS);
mPreference.setMin(getMin());
mPreference.setMax(getMax());
}
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
final LabeledSeekBarPreference pref = (LabeledSeekBarPreference) preference;
pref.setVisible(
PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)
&& getAvailabilityStatus() == AVAILABLE);
pref.setProgress(getSliderPosition());
}
@Override
public int getAvailabilityStatus() {
if (mSensitivityValues == null
|| mSensitivityValues.length < 2
|| !PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)) {
return UNSUPPORTED_ON_DEVICE;
}
return AVAILABLE;
}
@Override
public int getSliderPosition() {
return mSensitivityValues == null ? 0 : closestValueIndex(mSensitivityValues,
getCurrentSensitivityValue());
}
@Override
public boolean setSliderPosition(int position) {
if (mSensitivityValues == null || position < 0 || position >= mSensitivityValues.length) {
return false;
}
return Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS,
mSensitivityValues[position]);
}
@Override
public void onChange(Uri uri) {
if (mPreference != null) {
updateState(mPreference);
}
}
@Override
public int getMax() {
if (mSensitivityValues == null || mSensitivityValues.length == 0) {
return 0;
}
return mSensitivityValues.length - 1;
}
@Override
public int getMin() {
return 0;
}
private int getCurrentSensitivityValue() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS,
mContext.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerDurationMs));
}
private static int closestValueIndex(int[] values, int needle) {
int minDistance = Integer.MAX_VALUE;
int valueIndex = 0;
for (int i = 0; i < values.length; i++) {
int diff = Math.abs(values[i] - needle);
if (diff < minDistance) {
minDistance = diff;
valueIndex = i;
}
}
return valueIndex;
}
}R.array.config_longPressOnPowerDurationSettings 定義在 frameworks/base/core/res/res/values/config.xml ,
<!-- The possible UI options to be surfaced for configuring long press power on duration
action. Value set in config_longPressOnPowerDurationMs should be one of the available
options to allow users to restore default. -->
<integer-array name="config_longPressOnPowerDurationSettings">
<item>250</item>
<item>350</item>
<item>500</item>
<item>650</item>
<item>750</item>
</integer-array>到此這篇關(guān)于Android Settings 按住電源按鈕的文章就介紹到這了,更多相關(guān)Android Settings 按住電源按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)隱藏狀態(tài)欄及測(cè)試Activity是否活動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)隱藏狀態(tài)欄及測(cè)試Activity是否活動(dòng)的方法,涉及Android界面布局設(shè)置及Activity狀態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2016-10-10
Android 實(shí)現(xiàn)局部圖片滑動(dòng)指引效果
這篇文章主要介紹了Android 實(shí)現(xiàn)局部圖片滑動(dòng)指引效果的相關(guān)資料,需要的朋友可以參考下2017-01-01
android導(dǎo)入第三方j(luò)ar包報(bào)錯(cuò) 如何正確導(dǎo)入jar包
怎樣在android平臺(tái)上使用第三方j(luò)ar包,為什么我在引入了,編譯時(shí)沒(méi)有錯(cuò)誤,運(yùn)行時(shí)就有錯(cuò)誤,報(bào)無(wú)法實(shí)例化錯(cuò)誤,請(qǐng)問(wèn)這是什么原因,本文給于解決方法,需要了解的朋友可以參考下2012-12-12
Android提高之BLE開(kāi)發(fā)Android手機(jī)搜索iBeacon基站
這篇文章主要介紹了BLE開(kāi)發(fā)Android手機(jī)搜索iBeacon基站,需要的朋友可以參考下2014-08-08
Android開(kāi)發(fā)中Activity屬性設(shè)置小結(jié)
Android應(yīng)用開(kāi)發(fā)中會(huì)經(jīng)常遇到Activity組件的使用,下面就來(lái)講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開(kāi)發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過(guò)本文,你可以進(jìn)一步了接Android中Activity的運(yùn)作方式。2015-05-05
Android自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
flutter優(yōu)雅實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源示例詳解
這篇文章主要為大家介紹了flutter優(yōu)雅實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法
下面小編就為大家分享一篇Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android使用SharedPreferences存儲(chǔ)XML文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用SharedPreferences存儲(chǔ)XML文件的實(shí)現(xiàn)方法,實(shí)例分析了SharedPreferences類(lèi)的基本初始化與文件存儲(chǔ)相關(guān)技巧,需要的朋友可以參考下2016-07-07
Android 桌面Widget開(kāi)發(fā)要點(diǎn)解析(時(shí)間日期Widget)
總的來(lái)說(shuō),widget主要功能就是顯示一些信息。我們今天編寫(xiě)一個(gè)很簡(jiǎn)單的作為widget,顯示時(shí)間、日期、星期幾等信息。需要顯示時(shí)間信息,那就需要實(shí)時(shí)更新,一秒或者一分鐘更新一次2013-07-07

