注意: 寫該文章主要幫助自己記憶证舟,貼出來(lái)希望可以給有同樣問(wèn)題的人解惑拴曲,不喜勿噴,可以提意見(jiàn)哦。
一枷遂、待了解內(nèi)容
1樱衷、MediaButtonReceiver使用的情景
2、系統(tǒng)發(fā)送廣播到MediaButtonReceiver接收廣播流程的講解酒唉。
二矩桂、
1、MediaButtonReceiver使用的情景
Android源碼中并沒(méi)有MediaButtonReceiver這個(gè)類痪伦,MediaButtonReceiver繼承了BroadCastReceiver,即MediaButtonReceiver也就是一個(gè)簡(jiǎn)單的廣播接收者侄榴。但為什么單獨(dú)要講MediaButtonReceiver者呢,顧名思義网沾,該廣播接受者主要處理媒體按鈕的一些事件癞蚕,例如耳機(jī)中暫停,播放辉哥,上一曲,下一曲,音量調(diào)大碌燕,音量減小等咒劲。
例如:當(dāng)有如下幾個(gè)應(yīng)用圖片,本地音樂(lè)饲齐,藍(lán)牙音樂(lè)钉凌,收音機(jī)等,其中這些應(yīng)用中都有如下功能捂人,暫停甩骏,播放,上一個(gè)先慷,下一個(gè)饮笛,并且都注冊(cè)了語(yǔ)音操作廣播,當(dāng)操作這些功能的時(shí)候论熙,應(yīng)用都會(huì)做相應(yīng)的處理福青,為了避免語(yǔ)音操作播放,所有的應(yīng)用都接收到了該廣播脓诡,并執(zhí)行播放動(dòng)作无午,這個(gè)時(shí)候就需要利用MediaButtonReceiver的一個(gè)特別之處,只有當(dāng)前獲取焦點(diǎn)的應(yīng)用才會(huì)接收到該廣播祝谚。
2宪迟、系統(tǒng)發(fā)送廣播到MediaButtonReceiver接收廣播流程的講解。
注冊(cè)交惯、接收廣播簡(jiǎn)單實(shí)現(xiàn)次泽。
(1)實(shí)現(xiàn)MediaButtonReceiver代碼
public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event =
(KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
break;
}
}
}}
(2)實(shí)現(xiàn)廣播接收者之后穿仪,需要注冊(cè)。在AndroidManifest.xml中靜態(tài)注冊(cè)廣播意荤。因?yàn)樾枨髥?wèn)題啊片,這里只監(jiān)聽(tīng)了一個(gè)action,表示媒體按鍵按下的時(shí)候會(huì)觸發(fā)該廣播玖像,如果有其他需要紫谷,用戶還可以監(jiān)聽(tīng)這些廣播,如,android.intent.action.HEADSET_PLUG表示耳機(jī)插入與移除捐寥,android.media.AUDIO_BECOMING_NOISY笤昨, 但是這個(gè)廣播只是針對(duì)有線耳機(jī),或者無(wú)線耳機(jī)的手機(jī)斷開(kāi)連接的事件握恳,監(jiān)聽(tīng)不到有線耳機(jī)和藍(lán)牙耳機(jī)的接入瞒窒。
<receiver android:name="com.hwatong.music.MediaButtonReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
(3)廣播接收者實(shí)現(xiàn)和注冊(cè)后,接下來(lái)睡互,就需要對(duì)廣播接收者進(jìn)行封裝根竿。U盤音樂(lè),藍(lán)牙音樂(lè)就珠,一般是需要啟動(dòng)一個(gè)服務(wù)在后臺(tái)播放寇壳。
public class MusicService extends Service{
private AudioManager mAudioManager;
private ComponentName mComponentName;
@Override
public void onCreate() {
super.onCreate();
mComponentName = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
//音頻管理器主要是對(duì)AudioService相關(guān)方法的封裝,具體實(shí)現(xiàn)在AudioService中妻怎。
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
//這里只是講解MediaButtonReceiver,具體的播放代碼就不貼出來(lái)了
private void play(){
//當(dāng)應(yīng)用開(kāi)始播放的時(shí)候首先需要請(qǐng)求焦點(diǎn)壳炎,調(diào)用該方法后,原先獲取焦點(diǎn)的應(yīng)用會(huì)釋放焦點(diǎn)
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
//對(duì)媒體播放按鈕進(jìn)行封裝
if(mComponentName==null){
mComponentName = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
}
//注冊(cè)封裝的ComponentName
mAudioManager.registerMediaButtonEventReceiver(mComponentNa
me);
//省略具體播放內(nèi)容逼侦。匿辩。。
}
}
(4)榛丢,經(jīng)過(guò)(1)铲球、(2)、(3)步驟后晰赞,MediaButtonReceiver就已經(jīng)注冊(cè)成功稼病。
發(fā)送并分發(fā)廣播的實(shí)現(xiàn)
(1)發(fā)送媒體按鍵或者模擬系統(tǒng)按鍵。
//發(fā)送上一個(gè)媒體按鍵
sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
//發(fā)送下一個(gè)媒體按鍵
sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT);
//發(fā)送暫停媒體按鍵
sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE);
//發(fā)送播放媒體按鍵
sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY);
private void sendMediaKeyEvent(final int keyCode) {
//wait to handle onAbandonAudioFocus
mMediaKeyRunnable = new MediaKeyRunnbale(keyCode);
}
private MediaKeyRunnbale mMediaKeyRunnable;
private static class MediaKeyRunnbale implements Runnable {
int keyCode = -1;
public MediaKeyRunnbale(int keyCode) {
this.keyCode = keyCode;
}
@Override
public void run() {
//模擬按下抬起事件
sendEvent(KeyEvent.ACTION_DOWN, 0);
sendEvent(KeyEvent.ACTION_UP, 0);
}
private void sendEvent(int action, int flags) {
final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0;
//判斷是否是長(zhǎng)按事件
final KeyEvent ev = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), action, keyCode, repeatCount,
0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
flags | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,
InputDevice.SOURCE_KEYBOARD);
//調(diào)用InputManager中的injectInputEvent(...)方法掖鱼。
InputManager.getInstance().injectInputEvent(ev,
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}
}
(2)InputManager類位于frameworks\base\core\java\android\hardware\input中然走。上一步調(diào)用的方法如下。下面的對(duì)象mIm是 IInputManager戏挡,具體實(shí)現(xiàn)在ImputManagService芍瑞。
public boolean injectInputEvent(InputEvent event, int mode) {
if (event == null) {
throw new IllegalArgumentException("event must not be null");
}
if (mode != INJECT_INPUT_EVENT_MODE_ASYNC
&& mode != INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
&& mode != INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
throw new IllegalArgumentException("mode is invalid");
}
try {
return mIm.injectInputEvent(event, mode);
} catch (RemoteException ex) {
return false;
}
}
(3)InputManagerService位于frameworks\base\services\java\com\android\server\input目錄下。上一步調(diào)用方法如下褐墅。該方法中最主要的一步 result = nativeInjectInputEvent(...);該方法是一個(gè)本地方法拆檬。這里涉及到Java和C語(yǔ)言之間的調(diào)用洪己,c中的具體代碼就不貼出來(lái)了。
@Override // Binder call
public boolean injectInputEvent(InputEvent event, int mode) {
if (event == null) {
throw new IllegalArgumentException("event must not be null");
}
if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
&& mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
&& mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
throw new IllegalArgumentException("mode is invalid");
}
final int pid = Binder.getCallingPid();
final int uid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity();
final int result;
try {
result = nativeInjectInputEvent(mPtr, event, pid, uid, mode,
INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
} finally {
Binder.restoreCallingIdentity(ident);
}
switch (result) {
case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
throw new SecurityException(
"Injecting to another application requires INJECT_EVENTS permission");
case INPUT_EVENT_INJECTION_SUCCEEDED:
return true;
case INPUT_EVENT_INJECTION_TIMED_OUT:
Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
return false;
case INPUT_EVENT_INJECTION_FAILED:
default:
Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
return false;
}
}
(4)秩仆、第3步調(diào)用到本地方法码泛,本地方法經(jīng)過(guò)一番處理之后就會(huì)回調(diào)AudioService.java中的代碼猾封,AudioService位于frameworks\base\media\java\android\media目錄中
//==========================================================================================
// RemoteControl C代碼處理完畢后澄耍,就會(huì)回調(diào)該方法。關(guān)于Java代碼與本地代碼的相互調(diào)用晌缘,可以看另一篇文章《Java代碼與本地代碼相互調(diào)用主要事項(xiàng)》
//==========================================================================================
public void dispatchMediaKeyEvent(KeyEvent keyEvent) {
//參數(shù)1齐莲,表示KeyEvent,參數(shù)2表示是否喚醒屏幕。
filterMediaKeyEvent(keyEvent, false );
}
//filterMediaKeyEvent()方法具體實(shí)現(xiàn)如下磷箕,主要對(duì)于不同的KeyEvent進(jìn)行分發(fā)选酗。
private void filterMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
// sanity check on the incoming key event
//如果KeyEvent為空則直接返回
if (!isValidMediaKeyEvent(keyEvent)) {
Log.e(TAG, "not dispatching invalid media key event " + keyEvent);
return;
}
// event filtering for telephony
//來(lái)電或者通話的時(shí)候,需要調(diào)用dispatchMediaKeyEventForCalls方法
synchronized(mRingingLock) {
synchronized(mRCStack) {
if ((mMediaReceiverForCalls != null) &&
(mIsRinging || (getMode() == AudioSystem.MODE_IN_CALL))) {
dispatchMediaKeyEventForCalls(keyEvent, needWakeLock);
return;
}
}
}
// event filtering based on voice-based interactions
if (isValidVoiceInputKeyCode(keyEvent.getKeyCode())) {
filterVoiceInputKeyEvent(keyEvent, needWakeLock);
} else {
dispatchMediaKeyEvent(keyEvent, needWakeLock);
}
}
//對(duì)聲音按鍵進(jìn)行處理岳枷,主要對(duì)聲音按鍵按下抬起芒填,長(zhǎng)按短按的處理。
private void filterVoiceInputKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
if (DEBUG_RC) {
Log.v(TAG, "voice input key event: " + keyEvent + ", needWakeLock=" + needWakeLock);
}
int voiceButtonAction = VOICEBUTTON_ACTION_DISCARD_CURRENT_KEY_PRESS;
int keyAction = keyEvent.getAction();
synchronized (mVoiceEventLock) {
if (keyAction == KeyEvent.ACTION_DOWN) {
//表示短按
if (keyEvent.getRepeatCount() == 0) {
// initial down
mVoiceButtonDown = true;
mVoiceButtonHandled = false;
} else if (mVoiceButtonDown && !mVoiceButtonHandled
&& (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
// long-press, start voice-based interactions
mVoiceButtonHandled = true;
voiceButtonAction = VOICEBUTTON_ACTION_START_VOICE_INPUT;
}
} else if (keyAction == KeyEvent.ACTION_UP) {
if (mVoiceButtonDown) {
// voice button up
mVoiceButtonDown = false;
if (!mVoiceButtonHandled && !keyEvent.isCanceled()) {
voiceButtonAction = VOICEBUTTON_ACTION_SIMULATE_KEY_PRESS;
}
}
}
}//synchronized (mVoiceEventLock)
// take action after media button event filtering for voice-based interactions
switch (voiceButtonAction) {
case VOICEBUTTON_ACTION_DISCARD_CURRENT_KEY_PRESS:
if (DEBUG_RC) Log.v(TAG, " ignore key event");
break;
case VOICEBUTTON_ACTION_START_VOICE_INPUT:
if (DEBUG_RC) Log.v(TAG, " start voice-based interactions");
// then start the voice-based interactions
startVoiceBasedInteractions(needWakeLock);
break;
case VOICEBUTTON_ACTION_SIMULATE_KEY_PRESS:
if (DEBUG_RC) Log.v(TAG, " send simulated key event, wakelock=" + needWakeLock);
sendSimulatedMediaButtonEvent(keyEvent, needWakeLock);
break;
}
}
private void dispatchMediaKeyEventForCalls(KeyEvent keyEvent, boolean needWakeLock) {
Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
keyIntent.setPackage(mMediaReceiverForCalls.getPackageName());
if (needWakeLock) {
mMediaEventWakeLock.acquire();
keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, WAKELOCK_RELEASE_ON_FINISHED);
}
final long ident = Binder.clearCallingIdentity();
try {
mContext.sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL,
null, mKeyEventDone, mAudioHandler, Activity.RESULT_OK, null, null);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
//對(duì)普通按鍵進(jìn)行處理空繁,一般就是自定義的一些KeyEvent
private void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
if (needWakeLock) {
//喚醒屏幕
mMediaEventWakeLock.acquire();
}
//發(fā)送ACTION_MEDIA_BUTTON廣播
Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
synchronized(mRCStack) {
//mRCStack該對(duì)象是一個(gè)棧數(shù)據(jù)結(jié)構(gòu)殿衰,主要對(duì)PendingIntent進(jìn)行封裝
if (!mRCStack.empty()) {
// send the intent that was registered by the client
try {
//取出棧頂?shù)腜endingIntent,這里就是關(guān)鍵,為什么當(dāng)前獲取焦點(diǎn)的
界面才會(huì)收到廣播盛泡。
mRCStack.peek().mMediaIntent.send(mContext,
needWakeLock ? WAKELOCK_RELEASE_ON_FINISHED : 0 /*code*/,
keyIntent, AudioService.this, mAudioHandler);
} catch (CanceledException e) {
Log.e(TAG, "Error sending pending intent " + mRCStack.peek());
e.printStackTrace();
}
} else {
// legacy behavior when nobody registered their media button event receiver
// through AudioManager
if (needWakeLock) {
keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, WAKELOCK_RELEASE_ON_FINISHED);
}
final long ident = Binder.clearCallingIdentity();
try {
mContext.sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL,
null, mKeyEventDone,
mAudioHandler, Activity.RESULT_OK, null, null);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
}
}
(5)看完第(4)步大家可能不是很理解Pending是如何與RCStack產(chǎn)生聯(lián)系的闷祥,這里就來(lái)講解PendingIntent是如何入棧,如何出棧的傲诵。
當(dāng)客戶端調(diào)用
mAudioManager.registerMediaButtonEventReceiver(mComponentName);時(shí)凯砍,這時(shí)會(huì)間接調(diào)用frameworks\base\media\java\android\media目錄下的AudioService.java類中的方法pushMediaButtonReceiver,具體實(shí)現(xiàn)如下:
private void pushMediaButtonReceiver(PendingIntent mediaIntent, ComponentName target) {
// already at top of stack?
if (!mRCStack.empty() && mRCStack.peek().mMediaIntent.equals(mediaIntent)) {
return;
}
Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
RemoteControlStackEntry rcse = null;
boolean wasInsideStack = false;
while(stackIterator.hasNext()) {
rcse = (RemoteControlStackEntry)stackIterator.next();
if(rcse.mMediaIntent.equals(mediaIntent)) {
wasInsideStack = true;
stackIterator.remove();
break;
}
}
if (!wasInsideStack) {
rcse = new RemoteControlStackEntry(mediaIntent, target);
}
//主要代碼拴竹,入棧悟衩。
mRCStack.push(rcse);
// post message to persist the default media button receiver
mAudioHandler.sendMessage( mAudioHandler.obtainMessage(
MSG_PERSIST_MEDIABUTTONRECEIVER, 0, 0, target/*obj*/) );
}
當(dāng)客戶端調(diào)用
mAudioManager.unregisterMediaButtonEventReceiver(mComponentName);時(shí),這時(shí)會(huì)間接調(diào)用frameworks\base\media\java\android\media目錄下的AudioService.java類中的方法removeMediaButtonReceiverForPackage栓拜,具體實(shí)現(xiàn)如下:
private void removeMediaButtonReceiverForPackage(String packageName) {
synchronized(mRCStack) {
if (mRCStack.empty()) {
return;
} else {
RemoteControlStackEntry oldTop = mRCStack.peek();
Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
// iterate over the stack entries
while(stackIterator.hasNext()) {
RemoteControlStackEntry rcse = (RemoteControlStackEntry)stackIterator.next();
if (packageName.equalsIgnoreCase(rcse.mReceiverComponent.getPackageName())) {
// a stack entry is from the package being removed, remove it from the stack
stackIterator.remove();
rcse.unlinkToRcClientDeath();
}
}
if (mRCStack.empty()) {
// no saved media button receiver
mAudioHandler.sendMessage(
mAudioHandler.obtainMessage(MSG_PERSIST_MEDIABUTTONRECEIVER, 0, 0,
null));
} else if (oldTop != mRCStack.peek()) {
// the top of the stack has changed, save it in the system settings
// by posting a message to persist it
mAudioHandler.sendMessage(
mAudioHandler.obtainMessage(MSG_PERSIST_MEDIABUTTONRECEIVER, 0, 0,
mRCStack.peek().mReceiverComponent));
}
}
}
}