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

来源:互联网 发布:手机手柄连接软件 编辑:程序博客网 时间:2024/05/22 07:47

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

  TtsService extends Service

view plaincopy to clipboard
  1. private final RemoteCallbackList<ITtsCallback> mCallbacks  
  2.            = new RemoteCallbackList<ITtsCallback>();  
[java] view plaincopy
  1. private final RemoteCallbackList<ITtsCallback> mCallbacks            = new RemoteCallbackList<ITtsCallback>();  


view plaincopy to clipboard
  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.     }  
[java] view plaincopy
  1. private final android.speech.tts.ITts.Stub mBinder = new Stub() {        public int registerCallback(String packageName, ITtsCallback cb) {            if (cb != null) {                mCallbacks.register(cb);                mCallbacksMap.put(packageName, cb);                return TextToSpeech.SUCCESS;            }            return TextToSpeech.ERROR;        }        public int unregisterCallback(String packageName, ITtsCallback cb) {            if (cb != null) {                mCallbacksMap.remove(packageName);                mCallbacks.unregister(cb);                return TextToSpeech.SUCCESS;            }            return TextToSpeech.ERROR;        }        public int speak(String callingApp, String text, int queueMode, String[] params) {            ArrayList<String> speakingParams = new ArrayList<String>();            if (params != null) {                speakingParams = new ArrayList<String>(Arrays.asList(params));            }            return this.speak(callingApp, text, queueMode, speakingParams);        }  

view plaincopy to clipboard
  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. }  
[java] view plaincopy
  1. private void dispatchProcessingCompletedCallback(String packageName) {        ITtsCallback cb = mCallbacksMap.get(packageName);        if (cb == null){            return;        }        //Log.i("TtsService", "TTS callback: dispatch started");        // Broadcast to all clients the new value.        final int N = mCallbacks.beginBroadcast();        try {            cb.processingCompleted();        } catch (RemoteException e) {            // The RemoteCallbackList will take care of removing            // the dead object for us.        }        mCallbacks.finishBroadcast();        //Log.i("TtsService", "TTS callback: dispatch completed to " + N);    }  

view plaincopy to clipboard
  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. }  
[java] view plaincopy
  1. @Override    public void onDestroy() {        super.onDestroy();        // TODO replace the call to stopAll() with a method to clear absolutely all upcoming        // uses of the native synth, including synthesis to a file, and delete files for which        // synthesis was not complete.        stopAll();        // Unregister all callbacks.        mCallbacks.kill();    }  


在activity中

view plaincopy to clipboard
  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);  
[java] view plaincopy
  1. mITtscallback = new ITtsCallback.Stub() {                public void processingCompleted() throws RemoteException {                    if (listener != null) {                        listener.onProcessingCompleted();                    }                }            };  result = mITts.registerCallback(mPackageName, mITtscallback);  
0 0
原创粉丝点击