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

Android 耳机按钮监听

[日期:2012-11-28] 来源:blog.csdn.net/qinjuning  作者:qinjuning [字体:大 中 小]

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

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

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

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

IAudioService.aidl定义如下:

  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

  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对象永远处于栈顶

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

  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. } 

通过对前面的学习,我们知道了AudioManager内部利用一个栈来管理包括加入和移除ComponentName对象,新的疑问来了?这个MEDIA_BUTTON广播是如何分发的呢 ?

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款