gpt4 book ai didi

android - 使用CheckBoxPreference切换手机的声音,振动和互联网访问的开/关

转载 作者:行者123 更新时间:2023-12-03 01:53:58 39 4
gpt4 key购买 nike

如何使用Android中的CheckBoxPreference切换整个系统的声音,振动,数据连接和wifi?
我想在我的应用程序中正在运行 Activity 时禁用这些功能。

最佳答案

我想到了。

res/xml/preferences.xml中添加CheckBoxPreferences:

    <CheckBoxPreference
android:defaultValue="false"
android:key="pref_disableSoundAndVibration"
android:title="Disable sound and vibration" />
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_disableWifi"
android:title="Disable Wi-Fi" />

在您的MainActivity中,添加两个 bool(boolean) 成员来存储音频和wifi复选框状态,并为铃声状态(静音,振动,声音)添加一个整数。
protected void onCreate(Bundle savedInstanceState) {
...
mDisableSoundAndVibration = mPref.getBoolean("pref_disableSoundAndVibration", false);
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
mRingerMode = aManager.getRingerMode();

mDisableWifi = mPref.getBoolean("pref_disableWifi", false);
WifiManager wifiManager = (WifiManager)this.getSystemService(WIFI_SERVICE);
mWifiMode = wifiManager.isWifiEnabled();
...
if (savedInstanceState != null) {
mDisableSoundAndVibration = savedInstanceState.getBoolean("disableSoundAndVibration");
mDisableWifi = savedInstanceState.getBoolean("disableWifi");
...
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
...
mDisableSoundAndVibration = mPref.getBoolean("pref_disableSoundAndVibration", false);
mDisableWifi = mPref.getBoolean("pref_disableWifi", false);
...
}
protected void onSaveInstanceState(Bundle outState) {
...
outState.putBoolean("disableSoundAndVibration", mDisableSoundAndVibration);
outState.putInt("ringerMode", mRingerMode);
outState.putBoolean("disableWifi", mDisableWifi);
....
}

如果要暂时禁用声音和Wi-Fi并保持初始状态,请执行以下操作:
    if (mDisableSoundAndVibration){
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setRingerMode(aManager.RINGER_MODE_SILENT);
}
if (mDisableWifi){
WifiManager wifiManager = (WifiManager)this.getSystemService(this.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}

然后恢复初始值:
    if (mDisableSoundAndVibration){
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setRingerMode(mRingerMode);
}
if(mDisableWifi){
WifiManager wifiManager = (WifiManager)this.getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(mWifiMode);
}

关于android - 使用CheckBoxPreference切换手机的声音,振动和互联网访问的开/关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262175/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com