说明:我没有写实例代码,直接拿项目中的代码,有点懒了,这里我省略贴出两个aidl文件。
TtsService extends Service
- private final RemoteCallbackList<ITtsCallback> mCallbacks
- = new RemoteCallbackList<ITtsCallback>();
- private final RemoteCallbackList<ITtsCallback> mCallbacks = new RemoteCallbackList<ITtsCallback>();
- 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);
- }
- 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); }
- 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);
- }
- 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); }
- @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();
- }
- @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中
- mITtscallback = new ITtsCallback.Stub() {
- public void processingCompleted() throws RemoteException {
- if (listener != null) {
- listener.onProcessingCompleted();
- }
- }
- };
- result = mITts.registerCallback(mPackageName, mITtscallback);
- mITtscallback = new ITtsCallback.Stub() { public void processingCompleted() throws RemoteException { if (listener != null) { listener.onProcessingCompleted(); } } }; result = mITts.registerCallback(mPackageName, mITtscallback);