aidl 中通过RemoteCallbackList 运用到的回调机制: service回调activity的方法 .

 

说明:我没有写实例代码,直接拿项目中的代码,有点懒了,这里我省略贴出两个aidl文件。

  TtsService extends Service

  1. private final RemoteCallbackList<ITtsCallback> mCallbacks  
  2.            = new RemoteCallbackList<ITtsCallback>();  


  1. private final android.speech.tts.ITts.Stub mBinder = new Stub() {  
  2.   
  3.     public int registerCallback(String packageName, ITtsCallback cb) {  
  4.         if (cb != null) {  
  5.             mCallbacks.register(cb);  
  6.             mCallbacksMap.put(packageName, cb);  
  7.             return TextToSpeech.SUCCESS;  
  8.         }  
  9.         return TextToSpeech.ERROR;  
  10.     }  
  11.   
  12.     public int unregisterCallback(String packageName, ITtsCallback cb) {  
  13.         if (cb != null) {  
  14.             mCallbacksMap.remove(packageName);  
  15.             mCallbacks.unregister(cb);  
  16.             return TextToSpeech.SUCCESS;  
  17.         }  
  18.         return TextToSpeech.ERROR;  
  19.     }  
  20.   
  21.   
  22.     public int speak(String callingApp, String text, int queueMode, String[] params) {  
  23.         ArrayList<String> speakingParams = new ArrayList<String>();  
  24.         if (params != null) {  
  25.             speakingParams = new ArrayList<String>(Arrays.asList(params));  
  26.         }  
  27.         return this.speak(callingApp, text, queueMode, speakingParams);  
  28.     }  

  1. private void dispatchProcessingCompletedCallback(String packageName) {  
  2.     ITtsCallback cb = mCallbacksMap.get(packageName);  
  3.     if (cb == null){  
  4.         return;  
  5.     }  
  6.     //Log.i("TtsService", "TTS callback: dispatch started");   
  7.     // Broadcast to all clients the new value.   
  8.     final int N = mCallbacks.beginBroadcast();  
  9.     try {  
  10.         cb.processingCompleted();  
  11.     } catch (RemoteException e) {  
  12.         // The RemoteCallbackList will take care of removing   
  13.         // the dead object for us.   
  14.     }  
  15.     mCallbacks.finishBroadcast();  
  16.     //Log.i("TtsService", "TTS callback: dispatch completed to " + N);   
  17. }  

  1. @Override  
  2. public void onDestroy() {  
  3.     super.onDestroy();  
  4.   
  5.     // TODO replace the call to stopAll() with a method to clear absolutely all upcoming   
  6.     // uses of the native synth, including synthesis to a file, and delete files for which   
  7.     // synthesis was not complete.   
  8.     stopAll();  
  9.   
  10.     // Unregister all callbacks.   
  11.     mCallbacks.kill();  
  12. }  


在activity中

  1. mITtscallback = new ITtsCallback.Stub() {  
  2.                 public void processingCompleted() throws RemoteException {  
  3.                     if (listener != null) {  
  4.                         listener.onProcessingCompleted();  
  5.                     }  
  6.                 }  
  7.             };  
  8.   
  9.   result = mITts.registerCallback(mPackageName, mITtscallback);  

上面只是一个贴代码没有做任何说明,基本的意思我想大家也能通过代码来看懂。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值