你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中MediaButtonReceiver广播监听器的机制分析

[日期:2012-02-02] 来源:Linux社区  作者:qinjuning [字体:大 中 小]
第二步、 进入AudioManager.java进行查看 ,发现如下方法:

 

[java]
  1. //注册的方法为:   
  2. public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {  
  3.       //TODO enforce the rule about the receiver being declared in the manifest   
  4.       //我们继续查看getService()方法,看看IAudioService类到底是什么?   
  5.        IAudioService service = getService();  
  6.       try {  
  7.         //只是简单的调用了service的方法来完成注册,继续跟踪   
  8.           service.registerMediaButtonEventReceiver(eventReceiver);  
  9.        
  10.       } catch (RemoteException e) {  
  11.           Log.e(TAG, "Dead object in registerMediaButtonEventReceiver"+e);  
  12.       }  
  13. }  
  14. //取消注册的方法为   
  15. public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {  
  16.       IAudioService service = getService();    
  17.       try {  
  18.         //只是简单的调用了service的方法来取消注册,,继续跟踪   
  19.           service.unregisterMediaButtonEventReceiver(eventReceiver);  
  20.       } catch (RemoteException e) {  
  21.           Log.e(TAG, "Dead object in unregisterMediaButtonEventReceiver"+e);  
  22.       }  
  23.   }  


  找到getService()方法,其实现为:

[java]
  1.   //看看它到底是什么   
  2.   private static IAudioService getService()  
  3.     {  
  4.                // 单例模式,大家懂得   
  5.         if (sService != null) {  
  6.             return sService;  
  7.        }  
  8.        //了解Binder机制 以及AIDL文件的使用,就明白了这不过是通过AIDL文件定义的Java层Binder机制   
  9.         //b为IBinder基类接口   
  10.         IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);  
  11.        //强制转换后,sService不过是一个客户端对象,IAudioService就是aidl文件定义的接口了   
  12.         sService = IAudioService.Stub.asInterface(b);  
  13.        return sService;  
  14.     }      
  15. //sService对象的声明   
  16.          private static IAudioService sService; //单例模式,不足为奇了  


         我们知道了AudiaoManager只不过是一个傀儡,所有的方法都是由IAudioService 对象去实现的,通过它的构造方式,

  可以知道它应该是有AIDL文件形成的Binder机制, sService只是客户端对象,那么它的服务端对象在什么地方呢?

  也就是继承了IAudioService.Stub桩的类。

 

第三步、接下来我们需要找到该IAudioService.aidl文件和真正的服务端对象  

 

   IAudioService.aidl定义如下:

[java]
  1. package Android.media;  
  2.   
  3. import android.content.ComponentName;  
  4. import android.media.IAudioFocusDispatcher;  
  5. /** 
  6.  * {@hide} 
  7.  */  
  8. interface IAudioService {  
  9.       
  10.     void adjustVolume(int direction, int flags);  
  11.     void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags);  
  12.     void adjustStreamVolume(int streamType, int direction, int flags);          
  13.     void setStreamVolume(int streamType, int index, int flags);          
  14.     void setStreamSolo(int streamType, boolean state, IBinder cb);            
  15.     void setStreamMute(int streamType, boolean state, IBinder cb);        
  16.     int getStreamVolume(int streamType);          
  17.     int getStreamMaxVolume(int streamType);         
  18.     void setRingerMode(int ringerMode);          
  19.     int getRingerMode();  
  20.     void setVibrateSetting(int vibrateType, int vibrateSetting);          
  21.     int getVibrateSetting(int vibrateType);          
  22.     boolean shouldVibrate(int vibrateType);  
  23.     void setMode(int mode, IBinder cb);  
  24.     int getMode();  
  25.     oneway void playSoundEffect(int effectType);        
  26.     oneway void playSoundEffectVolume(int effectType, float volume);  
  27.     boolean loadSoundEffects();       
  28.     oneway void unloadSoundEffects();  
  29.     oneway void reloadAudioSettings();  
  30.     void setSpeakerphoneOn(boolean on);  
  31.     boolean isSpeakerphoneOn();  
  32.     void setBluetoothScoOn(boolean on);  
  33.     boolean isBluetoothScoOn();  
  34.     int requestAudioFocus(int mainStreamType, int durationHint, IBinder cb, IAudioFocusDispatcher l, String clientId);  
  35.     int abandonAudioFocus(IAudioFocusDispatcher l, String clientId);          
  36.     void unregisterAudioFocusClient(String clientId);  
  37.     void registerMediaButtonEventReceiver(in ComponentName eventReceiver);   //这个方法是我们需要弄懂的   
  38.     void unregisterMediaButtonEventReceiver(in ComponentName eventReceiver);  //这个方法也是是我们需要弄懂的   
  39.     void startBluetoothSco(IBinder cb);  
  40.     void stopBluetoothSco(IBinder cb);  
  41. }  

    

       真正的服务端对象就是继承了 IAudioService.Stub 桩的类,AudioService就是该服务端对象,其实AudioManager的

  所有操作都是由AudioService来实现的,它才是真正的老大。


第五步、   AudioService.java

[java]
  1. //AudioService类    
  2. public class AudioService extends IAudioService.Stub {  
  3.     //.....   
  4.     //仅仅列出我们需要的方法   
  5.     //这儿才是真正的注册MediaButtonReceiver的方法   
  6.     public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {  
  7.         Log.i(TAG, "  Remote Control   registerMediaButtonEventReceiver() for " + eventReceiver);  
  8.   
  9.         synchronized(mRCStack) {  
  10.           //调用它去实现注册ComponentName   
  11.             pushMediaButtonReceiver(eventReceiver);  
  12.         }  
  13.     }  
  14.       
  15.    //在查看pushMediaButtonReceiver()方法  先理解一下两个知识点,很重要的。   
  16.     //RemoteControlStackEntry内部类不过是对ComponentName类的进一步封装(感觉没必要在加一层进行封装了)    
  17.     private static class RemoteControlStackEntry {  
  18.         public ComponentName mReceiverComponent;// 属性   
  19.           //TODO implement registration expiration?   
  20.         //public int mRegistrationTime;   
  21.   
  22.         public RemoteControlStackEntry() {  
  23.         }  
  24.   
  25.         public RemoteControlStackEntry(ComponentName r) {  
  26.             mReceiverComponent = r;// 构造函数赋值给mReceiverComponent对象   
  27.         }  
  28.     }  
  29.       
  30.    //采用了栈存储结构(先进后出)来保存所有RemoteControlStackEntry对象,也就是保存了ComponentName对象   
  31.     private Stack<RemoteControlStackEntry> mRCStack = new Stack<RemoteControlStackEntry>();  
  32.      
  33.    //回到pushMediaButtonReceiver()查看,这下该拨开云雾了吧,继续学习   
  34.    private void pushMediaButtonReceiver(ComponentName newReceiver) {  
  35.      // already at top of stack?   
  36.         //采用了一个栈(前面我们介绍的知识点)来保存所有注册的ComponentName对象   
  37.         //如果当前栈不为空并且栈顶的对象与新注册的ComponentName对象一样,不做任何事,直接返回   
  38.         if (!mRCStack.empty() && mRCStack.peek().mReceiverComponent.equals(newReceiver)) {  
  39.             return;  
  40.         }  
  41.         //获得mRCStack栈的迭代器   
  42.         Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();  
  43.         //循环   
  44.         while(stackIterator.hasNext()) {  
  45.           RemoteControlStackEntry rcse = (RemoteControlStackEntry)stackIterator.next();  
  46.           //如果当前栈内保存该新注册的ComponentName对象,将它移除,跳出循环   
  47.             if(rcse.mReceiverComponent.equals(newReceiver)) {  
  48.                 mRCStack.remove(rcse);  
  49.                 break;  
  50.             }  
  51.         }  
  52.       //将新注册的ComponentName对象放入栈顶   
  53.         mRCStack.push(new RemoteControlStackEntry(newReceiver));  
  54.     }  
  55. }  

小结一下: 


         栈(mRCStack)维护了所有CompoentName对象,对每个CompoentName对象,保证它有且仅有一个,

     新注册的CompoentName对象永远处于栈顶    


 

 我们看下取消注册的方法:

[java]
  1. //我们看下取消注册的方法   
  2. /** see AudioManager.unregisterMediaButtonEventReceiver(ComponentName eventReceiver) */  
  3. public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {  
  4.     Log.i(TAG, "  Remote Control   unregisterMediaButtonEventReceiver() for " + eventReceiver);  
  5.   
  6.     synchronized(mRCStack) {  
  7.          //调用removeMediaButtonReceiver方法去实现   
  8.         removeMediaButtonReceiver(eventReceiver);  
  9.     }  
  10. }  
  11.   
  12. private void removeMediaButtonReceiver(ComponentName newReceiver) {  
  13.     Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();  
  14.     while(stackIterator.hasNext()) {  
  15.          //获得mRCStack栈的迭代器   
  16.         RemoteControlStackEntry rcse = (RemoteControlStackEntry)stackIterator.next();  
  17.         //如果存在该对象,则移除,跳出循环   
  18.         if(rcse.mReceiverComponent.equals(newReceiver)) {  
  19.             mRCStack.remove(rcse);  
  20.             break;  
  21.         }  
  22.     }  
  23. }  
linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (2)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 2 楼
* 匿名 发表于 2012/10/31 15:40:09
楼主你好,帖子写的很好,我想问一下 ,源码AudioService中判断是来电就不做处理,那么在外面注册的广播就收不到了,那通过耳机按钮来实现电话接听是如何实现的。
第 1 楼
* 匿名 发表于 2012/10/19 19:25:35
很乱