弱龄寄事外,委怀在琴书。这篇文章主要讲述Android 触摸提示音相关的知识,希望能为你提供帮助。
本文转载自:http://blog.csdn.net/Jin_HeZai/article/details/46791567
近期任务,涉及android触摸提示音。
首先,定位源码目标。很显然的,在原生的设置的声音功能页里面就包含了触摸音的开关。
那么我们找到对应的java代码,SoundSettings.java
package com.android.settings;
import java.util.List;
public class SoundSettings extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener {
- 1
- 2
- 3
- 4
- 5
- 6
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver resolver = getContentResolver();
int activePhoneType = TelephonyManager.getDefault().getCurrentPhoneType();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
addPreferencesFromResource(R.xml.sound_settings);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
这里我通过查找中文string资源文件,标示了xml文件的一些item的title。
<
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/sound_settings"
android:key="sound_settings"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
<
!-- 音量-->
<
com.android.settings.RingerVolumePreference
android:key="ring_volume"
android:title="@string/all_volume_title"
android:dialogTitle="@string/all_volume_title"
android:persistent="false"
android:streamType="ring" />
<
!-- 音乐效果-->
<
Preference
android:key="musicfx"
android:title="@string/musicfx_title">
<
intent android:targetPackage="com.android.musicfx"
android:targetClass="com.android.musicfx.ControlPanelPicker" />
<
/Preference>
<
!-- 来电铃声和振动-->
<
PreferenceCategory
android:key="category_calls_and_notification"
android:title="@string/sound_category_call_ringtone_vibrate_title"/>
<
!-- Do not nest these, or removals in code will break
手机铃声
-->
<
com.android.settings.DefaultRingtonePreference
android:key="ringtone"
android:title="@string/ringtone_title"
android:dialogTitle="@string/ringtone_title"
android:persistent="false"
android:ringtoneType="ringtone" />
<
!-- 响铃时振动-->
<
CheckBoxPreference
android:key="vibrate_when_ringing"
android:title="@string/vibrate_when_ringing_title"
android:persistent="false" />
<
!-- 系统-->
<
PreferenceCategory
android:title="@string/sound_category_system_title"/>
<
!-- Do not nest these, or removals in code will break -->
<
!-- 默认通知提示音-->
<
com.android.settings.DefaultRingtonePreference
android:key="notification_sound"
android:title="@string/notification_sound_title"
android:dialogTitle="@string/notification_sound_dialog_title"
android:persistent="false"
android:ringtoneType="notification" />
<
!-- 拨号键盘触摸音效-->
<
CheckBoxPreference
android:key="dtmf_tone"
android:title="@string/dtmf_tone_enable_title"
android:defaultValue="https://www.songbingjia.com/android/true" />
<
!-- 触摸提示音-->
<
CheckBoxPreference
android:key="sound_effects"
android:title="@string/sound_effects_enable_title"
android:defaultValue="https://www.songbingjia.com/android/true" />
<
!-- 锁屏提示音-->
<
CheckBoxPreference
android:key="lock_sounds"
android:title="@string/lock_sounds_enable_title"
android:defaultValue="https://www.songbingjia.com/android/true" />
<
!-- 触摸时振动-->
<
CheckBoxPreference
android:key="haptic_feedback"
android:title="@string/haptic_feedback_enable_title"
android:defaultValue="https://www.songbingjia.com/android/true" />
<
!-- 紧急提示音-->
<
ListPreference
android:key="emergency_tone"
android:title="@string/emergency_tone_title"
android:entries="@array/emergency_tone_entries"
android:entryValues="@array/emergency_tone_values" />
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
private static final String KEY_SOUND_EFFECTS = "sound_effects";
- 1
- 2
// 触摸提示音相关
mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
mSoundEffects.setPersistent(false);
mSoundEffects.setChecked(Settings.System.getInt(resolver,
Settings.System.SOUND_EFFECTS_ENABLED, 1) != 0);
- 1
- 2
- 3
- 4
- 5
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mVibrateWhenRinging) {
Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING,
mVibrateWhenRinging.isChecked() ? 1 : 0);
} else if (preference == mDtmfTone) {
Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
mDtmfTone.isChecked() ? 1 : 0);
} else if (preference == mSoundEffects) {
if (mSoundEffects.isChecked()) {
mAudioManager.loadSoundEffects();
} else {
mAudioManager.unloadSoundEffects();
}
Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
mSoundEffects.isChecked() ? 1 : 0);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
首先,触摸提示音,是对所有的view的点击时间都会有触发效果,那么我们看看View.java的类。
View.java有将近2万行,想研究的彻底很明显是不现实,或者说,不轻松的。那么我们搜素SOUND_EFFECTS_ENABLED 这个关键字
/**
* View flag indicating whether this view should have sound effects enabled
* for events such as clicking and touching.
*/
public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
- 1
- 2
- 3
- 4
- 5
/**
* Set whether this view should have sound effects enabled for events such as
* clicking and touching.
*
* <
p>
You may wish to disable sound effects for a view if you already play sounds,
* for instance, a dial key that plays dtmf tones.
*
* @param soundEffectsEnabled whether sound effects are enabled for this view.
* @see #isSoundEffectsEnabled()
* @see #playSoundEffect(int)
* @attr ref android.R.styleable#View_soundEffectsEnabled
*/
public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
}/**
* @return whether this view should have sound effects enabled for events such as
*clicking and touching.
*
* @see #setSoundEffectsEnabled(boolean)
* @see #playSoundEffect(int)
* @attr ref android.R.styleable#View_soundEffectsEnabled
*/
@ViewDebug.ExportedProperty
public boolean isSoundEffectsEnabled() {
return SOUND_EFFECTS_ENABLED == (mViewFlags &
SOUND_EFFECTS_ENABLED);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
/**
* Play a sound effect for this view.
*
* <
p>
The framework will play sound effects for some built in actions, such as
* clicking, but you may wish to play these effects in your widget,
* for instance, for internal navigation.
*
* <
p>
The sound effect will only be played if sound effects are enabled by the user, and
* {@link #isSoundEffectsEnabled()} is true.
*
* @param soundConstant One of the constants defined in {@link SoundEffectConstants}
*/
public void playSoundEffect(int soundConstant) {
if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
return;
}
mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
/** @see AudioManager#playSoundEffect(int) */
public void playSoundEffect(int effectType) {
playSoundEffectVolume(effectType, -1.0f);
}/** @see AudioManager#playSoundEffect(int, float) */
public void playSoundEffectVolume(int effectType, float volume) {
sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE,
effectType, (int) (volume * 1000), null, 0);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
对应的是在Android system/media/audio/ui Effect_Tick.ogg的文件。
想要修改它的话,可以在编译room的时候替换了它,或者push 一个新的同名文件。
推荐阅读
- android studio 导出的jar中没有主清单属性
- WebApplicationContext介绍
- 短视频如何制作(如何下载短视频?常用的短视频录制和剪辑App有哪些?)
- Appium 解决中文输入问题
- 安装Xampp-配置appche,mysql运行环境遇到的坑(转)
- BeanFactory和ApplicationContext介绍
- 调用Linux的busybox,通过linux命令来获取AndRoidIP
- mac 安装appium 1.6.5
- appium的各种