- //注册的方法为:
- public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {
- //TODO enforce the rule about the receiver being declared in the manifest
- //我们继续查看getService()方法,看看IAudioService类到底是什么?
- IAudioService service = getService();
- try {
- //只是简单的调用了service的方法来完成注册,继续跟踪
- service.registerMediaButtonEventReceiver(eventReceiver);
- } catch (RemoteException e) {
- Log.e(TAG, "Dead object in registerMediaButtonEventReceiver"+e);
- }
- }
- //取消注册的方法为
- public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {
- IAudioService service = getService();
- try {
- //只是简单的调用了service的方法来取消注册,,继续跟踪
- service.unregisterMediaButtonEventReceiver(eventReceiver);
- } catch (RemoteException e) {
- Log.e(TAG, "Dead object in unregisterMediaButtonEventReceiver"+e);
- }
- }
找到getService()方法,其实现为:
- //看看它到底是什么
- private static IAudioService getService()
- {
- // 单例模式,大家懂得
- if (sService != null) {
- return sService;
- }
- //了解Binder机制 以及AIDL文件的使用,就明白了这不过是通过AIDL文件定义的Java层Binder机制
- //b为IBinder基类接口
- IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
- //强制转换后,sService不过是一个客户端对象,IAudioService就是aidl文件定义的接口了
- sService = IAudioService.Stub.asInterface(b);
- return sService;
- }
- //sService对象的声明
- private static IAudioService sService; //单例模式,不足为奇了
我们知道了AudiaoManager只不过是一个傀儡,所有的方法都是由IAudioService 对象去实现的,通过它的构造方式,
可以知道它应该是有AIDL文件形成的Binder机制, sService只是客户端对象,那么它的服务端对象在什么地方呢?
也就是继承了IAudioService.Stub桩的类。
第三步、接下来我们需要找到该IAudioService.aidl文件和真正的服务端对象
IAudioService.aidl定义如下:
- package Android.media;
- import android.content.ComponentName;
- import android.media.IAudioFocusDispatcher;
- /**
- * {@hide}
- */
- interface IAudioService {
- void adjustVolume(int direction, int flags);
- void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags);
- void adjustStreamVolume(int streamType, int direction, int flags);
- void setStreamVolume(int streamType, int index, int flags);
- void setStreamSolo(int streamType, boolean state, IBinder cb);
- void setStreamMute(int streamType, boolean state, IBinder cb);
- int getStreamVolume(int streamType);
- int getStreamMaxVolume(int streamType);
- void setRingerMode(int ringerMode);
- int getRingerMode();
- void setVibrateSetting(int vibrateType, int vibrateSetting);
- int getVibrateSetting(int vibrateType);
- boolean shouldVibrate(int vibrateType);
- void setMode(int mode, IBinder cb);
- int getMode();
- oneway void playSoundEffect(int effectType);
- oneway void playSoundEffectVolume(int effectType, float volume);
- boolean loadSoundEffects();
- oneway void unloadSoundEffects();
- oneway void reloadAudioSettings();
- void setSpeakerphoneOn(boolean on);
- boolean isSpeakerphoneOn();
- void setBluetoothScoOn(boolean on);
- boolean isBluetoothScoOn();
- int requestAudioFocus(int mainStreamType, int durationHint, IBinder cb, IAudioFocusDispatcher l, String clientId);
- int abandonAudioFocus(IAudioFocusDispatcher l, String clientId);
- void unregisterAudioFocusClient(String clientId);
- void registerMediaButtonEventReceiver(in ComponentName eventReceiver); //这个方法是我们需要弄懂的
- void unregisterMediaButtonEventReceiver(in ComponentName eventReceiver); //这个方法也是是我们需要弄懂的
- void startBluetoothSco(IBinder cb);
- void stopBluetoothSco(IBinder cb);
- }
真正的服务端对象就是继承了 IAudioService.Stub 桩的类,AudioService就是该服务端对象,其实AudioManager的
所有操作都是由AudioService来实现的,它才是真正的老大。
第五步、 AudioService.java
- //AudioService类
- public class AudioService extends IAudioService.Stub {
- //.....
- //仅仅列出我们需要的方法
- //这儿才是真正的注册MediaButtonReceiver的方法
- public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {
- Log.i(TAG, " Remote Control registerMediaButtonEventReceiver() for " + eventReceiver);
- synchronized(mRCStack) {
- //调用它去实现注册ComponentName
- pushMediaButtonReceiver(eventReceiver);
- }
- }
- //在查看pushMediaButtonReceiver()方法 先理解一下两个知识点,很重要的。
- //RemoteControlStackEntry内部类不过是对ComponentName类的进一步封装(感觉没必要在加一层进行封装了)
- private static class RemoteControlStackEntry {
- public ComponentName mReceiverComponent;// 属性
- //TODO implement registration expiration?
- //public int mRegistrationTime;
- public RemoteControlStackEntry() {
- }
- public RemoteControlStackEntry(ComponentName r) {
- mReceiverComponent = r;// 构造函数赋值给mReceiverComponent对象
- }
- }
- //采用了栈存储结构(先进后出)来保存所有RemoteControlStackEntry对象,也就是保存了ComponentName对象
- private Stack<RemoteControlStackEntry> mRCStack = new Stack<RemoteControlStackEntry>();
- //回到pushMediaButtonReceiver()查看,这下该拨开云雾了吧,继续学习
- private void pushMediaButtonReceiver(ComponentName newReceiver) {
- // already at top of stack?
- //采用了一个栈(前面我们介绍的知识点)来保存所有注册的ComponentName对象
- //如果当前栈不为空并且栈顶的对象与新注册的ComponentName对象一样,不做任何事,直接返回
- if (!mRCStack.empty() && mRCStack.peek().mReceiverComponent.equals(newReceiver)) {
- return;
- }
- //获得mRCStack栈的迭代器
- Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
- //循环
- while(stackIterator.hasNext()) {
- RemoteControlStackEntry rcse = (RemoteControlStackEntry)stackIterator.next();
- //如果当前栈内保存该新注册的ComponentName对象,将它移除,跳出循环
- if(rcse.mReceiverComponent.equals(newReceiver)) {
- mRCStack.remove(rcse);
- break;
- }
- }
- //将新注册的ComponentName对象放入栈顶
- mRCStack.push(new RemoteControlStackEntry(newReceiver));
- }
- }
小结一下:
栈(mRCStack)维护了所有CompoentName对象,对每个CompoentName对象,保证它有且仅有一个,
新注册的CompoentName对象永远处于栈顶
我们看下取消注册的方法:
- //我们看下取消注册的方法
- /** see AudioManager.unregisterMediaButtonEventReceiver(ComponentName eventReceiver) */
- public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {
- Log.i(TAG, " Remote Control unregisterMediaButtonEventReceiver() for " + eventReceiver);
- synchronized(mRCStack) {
- //调用removeMediaButtonReceiver方法去实现
- removeMediaButtonReceiver(eventReceiver);
- }
- }
- private void removeMediaButtonReceiver(ComponentName newReceiver) {
- Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
- while(stackIterator.hasNext()) {
- //获得mRCStack栈的迭代器
- RemoteControlStackEntry rcse = (RemoteControlStackEntry)stackIterator.next();
- //如果存在该对象,则移除,跳出循环
- if(rcse.mReceiverComponent.equals(newReceiver)) {
- mRCStack.remove(rcse);
- break;
- }
- }
- }
