最近在调机器的音量部分,先做记录如下(都是基于MTK的75代码,其他的应该差不多):
目标1:一键调出音量调节界面
第一步:在PhoneWindowManager.java这个文件里面的interceptKeyBeforeQueueing的case中找到或者定义你要用的按键
比如:KeyEvent.KEYCODE_VOLUME_DOWN:
说到KEYCODE_VOLUME_DOWN我们先暂停下,先来学习下系统默认的音量调节的流程了:
首先系统在收到KEYCODE_VOLUME_DOWN之后,就走到它自己的case里面:
......................................................
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (down) {
if (isScreenOn && !mVolumeDownKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mVolumeDownKeyTriggered = true;
mVolumeDownKeyTime = event.getDownTime();
mVolumeDownKeyConsumedByScreenshotChord = false;
cancelPendingPowerKeyAction();
interceptScreenshotChord();
}
} else {
mVolumeDownKeyTriggered = false;
cancelPendingScreenshotChordAction();
}
if (down) {
ITelephony telephonyService = getTelephonyService();
if (telephonyService != null) {
try {
if (telephonyService.isRinging()) {//这个就是判断在响铃中,然后一键静音
telephonyService.silenceRinger();
// And *don't* pass this key thru to the current activity
// (which is probably the InCallScreen.)
result &= ~ACTION_PASS_TO_USER;
break;
}
if (telephonyService.isOffhook()//这个就是判断在通话中
&& (result & ACTION_PASS_TO_USER) == 0) {
// If we are in call but we decided not to pass the key to
// the application, handle the volume change here.
handleVolumeKey(AudioManager.STREAM_VOICE_CALL, keyCode);
break;
}
......................................................
其实最重要的函数是handleVolumeKey
void handleVolumeKey(int stream, int keycode) {
IAudioService audioService = getAudioService();
if (audioService == null) {
return;
}
try {
// since audio is playing, we shouldn't have to hold a wake lock
// during the call, but we do it as a precaution for the rare possibility
// that the music stops right before we call this
// TODO: Actually handle MUTE.
mBroadcastWakeLock.acquire();
audioService.adjustStreamVolume(stream,//这个函数就是调用系统的调节音量的
keycode == KeyEvent.KEYCODE_VOLUME_UP
? AudioManager.ADJUST_RAISE
: AudioManager.ADJUST_LOWER,
0);
} catch (RemoteException e) {
Log.w(TAG, "IAudioService.adjustStreamVolume() threw RemoteException " + e);
} finally {
mBroadcastWakeLock.release();
}
}
是adjustStreamVolume这个系统调节音量的,那这个界面具体是咋样的呢?
就需要看VolumePanel.java其实在这个里面就是一个dialog,这个dialog是不能获取到焦点的。也就是说在任意界面调出这个音量调节界面,你没有办法用其他键操作的,
比如:左右方向键。这个不符合我们的需求。
所以就需要自己去实现这个功能了
接暂停前的,找到KEYCODE_VOLUME_DOWN,我们在
下面用上我们的代码 volume_handleVolumeKey(AudioManager.STREAM_VOICE_CALL);
##################################################
void volume_handleVolumeKey(int stream) {
IAudioService audioService = getAudioService();
Log.w(TAG, "ysfeagle##stream = " + stream);
if (audioService == null) {
return;
}
Log.w(TAG, "ysfeagle##startActivity" );
Intent intent =new Intent(mContext,Volume.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("vol_stream", stream);
mContext.startActivity(intent);
}
#########################################################
这个就是自己写了一个activity同样的volume.java放在跟PhoneWindowManager.java同样的目录下,也就是
frameworks\base\policy\src\com\android\internal\policy\impl
它的布局文件可以放到frameworks\base\core\res\res\layout下面,然后在frameworks\base\core\res下面的AndroidManifest.xml添加
<activity
android:name="com.android.internal.policy.impl.Volume"
android:label="@string/title_activity_volume"
android:theme="@android:style/Theme.Dialog">
</activity>