说明:我没有写实例代码,直接拿项目中的代码,有点懒了,这里我省略贴出两个aidl文件。
TtsService extends Service
-
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 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();
-
}
在activity中
-
mITtscallback = new ITtsCallback.Stub() {
-
public void processingCompleted() throws RemoteException {
-
if (listener != null) {
-
listener.onProcessingCompleted();
-
}
-
}
-
};
-
-
result = mITts.registerCallback(mPackageName, mITtscallback);
上面只是一个贴代码没有做任何说明,基本的意思我想大家也能通过代码来看懂。
-
// int N = mCallbacks.beginBroadcast();
-
// try {
-
// for (int i = 0; i < N; i++) {
-
// mCallbacks.getBroadcastItem(i).showResult(mSlot);
-
// }
-
// } catch (RemoteException e) {
-
// Log("" + e);
-
// }
-
// mCallbacks.finishBroadcast();
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上传一个写的工作中用到的demo
-
package com.pateo.aidl;
-
-
interface ICallback {
-
-
void showResult(String result);
-
-
}
-
-
-
package com.pateo.aidl;
-
import com.pateo.aidl.ICallback;
-
-
interface IMyService {
-
-
void init(String packageName,String slot);
-
void registerCallback(String packageName,ICallback cb);
-
void unregisterCallback(String packageName,ICallback cb);
-
-
}
-
package com.pateo.service;
-
-
import java.util.HashMap;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.Handler;
-
import android.os.IBinder;
-
import android.os.Message;
-
import android.os.RemoteCallbackList;
-
import android.os.RemoteException;
-
-
import com.pateo.aidl.ICallback;
-
import com.pateo.aidl.IMyService;
-
-
public class VuiService extends Service {
-
-
private RemoteCallbackList<ICallback> mCallbacks = new RemoteCallbackList<ICallback>();
-
private HashMap<String, ICallback> mCallbacksMap = new HashMap<String, ICallback>();
-
private String mSlot = "";
-
private String mPackageName = "";
-
-
@Override
-
public void onStart(Intent intent, int startId) {
-
super.onStart(intent, startId);
-
}
-
-
@Override
-
public IBinder onBind(Intent intent) {
-
return remoteBinder;
-
}
-
-
@Override
-
public void onDestroy() {
-
mHandler.removeMessages(0);
-
mCallbacks.kill();
-
super.onDestroy();
-
}
-
-
@Override
-
public boolean onUnbind(Intent intent) {
-
return super.onUnbind(intent);
-
}
-
-
public void onRebind(Intent intent) {
-
super.onRebind(intent);
-
}
-
-
private IMyService.Stub remoteBinder = new IMyService.Stub() {
-
-
@Override
-
public void init(String packageName,String slot) throws RemoteException {
-
mSlot = slot;
-
mPackageName = packageName;
-
-
//?£?a?aê????ˉê?±e£??aà?μ?4000oá???àμ±óú??óèμ?ê?±e1y3ìμ?ê±??£??a???éò??úê?±e?á1?à???è¥μ÷ó?
-
mHandler.sendEmptyMessageDelayed(0, 4000);
-
}
-
-
@Override
-
public void unregisterCallback(String packageName, ICallback cb) {
-
if (cb != null) {
-
mCallbacks.unregister(cb);
-
mCallbacksMap.remove(packageName);
-
}
-
}
-
-
//°ü??×¢2áμ?·?ê?£??a?ù?í±ü?aá?oü?àó?ó?×¢2á??è¥??μ÷£??aà?í?1yó?ó???óèμ?packageNameà??¥????ì???μ÷??ò???ó?ó?μ?callback
-
@Override
-
public void registerCallback(String packageName, ICallback cb) {
-
if (cb != null) {
-
mCallbacks.register(cb);
-
mCallbacksMap.put(packageName, cb);
-
}
-
}
-
};
-
-
//?aà?í?1yó?ó???óèμ?packageNameà??¥????ì???μ÷??ò???ó?ó?μ?callback
-
private void dispatchProcessingCompletedCallback() {
-
ICallback cb = mCallbacksMap.get(mPackageName);
-
if (cb == null){
-
return;
-
}
-
final int N = mCallbacks.beginBroadcast();
-
try {
-
cb.showResult(mSlot);
-
} catch (RemoteException e) {
-
}
-
mCallbacks.finishBroadcast();
-
}
-
-
private Handler mHandler = new Handler() {
-
@Override
-
public void handleMessage(Message msg) {
-
dispatchProcessingCompletedCallback();
-
super.handleMessage(msg);
-
}
-
};
-
}
|