android 默认按键音开关的值存储位置在
frameworks/base/packages/SettingsProvider/res/values/defaults.xml
,
<!-- Default for UI touch sounds enabled -->
<bool name="def_sound_effects_enabled">true</bool>
true 为打开; false 为关闭 。
查看 AN 8.0 源码,是在 frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
中读取这个值,
private void loadUISoundEffectsSettings(SQLiteStatement stmt) {
loadBooleanSetting(stmt, Settings.System.DTMF_TONE_WHEN_DIALING,
R.bool.def_dtmf_tones_enabled);
loadBooleanSetting(stmt, Settings.System.SOUND_EFFECTS_ENABLED,
R.bool.def_sound_effects_enabled);
loadBooleanSetting(stmt, Settings.System.HAPTIC_FEEDBACK_ENABLED,
R.bool.def_haptic_feedback);
loadIntegerSetting(stmt, Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
R.integer.def_lockscreen_sounds_enabled);
}
在 frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
中根据该值来判断是否加载按键音,
public void restoreValue(Context context, ContentResolver cr, ContentValues contentValues,
Uri destination, String name, String value) {
// Will we need a post-restore broadcast for this element?
String oldValue = null;
boolean sendBroadcast = false;
final SettingsLookup table;
if (destination.equals(Settings.Secure.CONTENT_URI)) {
table = sSecureLookup;
} else if (destination.equals(Settings.System.CONTENT_URI)) {
table = sSystemLookup;
} else { /* must be GLOBAL; this was preflighted by the caller */
table = sGlobalLookup;
}
if (sBroadcastOnRestore.contains(name)) {
// TODO: http://b/22388012
oldValue = table.lookup(cr, name, UserHandle.USER_SYSTEM);
sendBroadcast = true;
}
try {
if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
setBrightness(Integer.parseInt(value));
// fall through to the ordinary write to settings
} else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
setSoundEffects(Integer.parseInt(value) == 1);
// fall through to the ordinary write to settings
} else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
setGpsLocation(value);
return;
} else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
setAutoRestore(Integer.parseInt(value) == 1);
} else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
return;
} else if (Settings.System.RINGTONE.equals(name)
|| Settings.System.NOTIFICATION_SOUND.equals(name)) {
setRingtone(name, value);
return;
}
// Default case: write the restored value to settings
contentValues.clear();
contentValues.put(Settings.NameValueTable.NAME, name);
contentValues.put(Settings.NameValueTable.VALUE, value);
cr.insert(destination, contentValues);
} catch (Exception e) {
// If we fail to apply the setting, by definition nothing happened
sendBroadcast = false;
} finally {
// If this was an element of interest, send the "we just restored it"
// broadcast with the historical value now that the new value has
// been committed and observers kicked off.
if (sendBroadcast) {
Intent intent = new Intent(Intent.ACTION_SETTING_RESTORED)
.setPackage("android").addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)
.putExtra(Intent.EXTRA_SETTING_NAME, name)
.putExtra(Intent.EXTRA_SETTING_NEW_VALUE, value)
.putExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE, oldValue);
context.sendBroadcastAsUser(intent, UserHandle.SYSTEM, null);
}
}
}
最后调用 AudioManager
接口实现按键音效果,
private AudioManager mAudioManager;
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
private void setSoundEffects(boolean enable) {
if (enable) {
mAudioManager.loadSoundEffects();
} else {
mAudioManager.unloadSoundEffects();
}
}