实现Android语音识别服务接口 RecognitionService的方法

之前的一篇文章介绍过SpeechRecognizer类,该类能够作为对外的一个接口,并通过Intent传递一个ComponentName获取可支持语音识别的功能的服务,一般的用户手机中假设安装了语音识别的一些软件,就会拥有这种能力,可是假设开发人员希望自己通过某公司的sdk集成自己的语音识别服务,那么就须要实现RecognitionService这个类。

这个类是个抽象类,须要开发人员完毕当中的几个抽象方法。以下的代码凝视中对每一个方法进行了说明。

<pre name="code" class="java">public class BDRecognitionService extends RecognitionService {

    @Override
public void onCreate() {
super.onCreate();
// 在调用服务时进行一次
} @Override
protected void onStartListening(Intent recognizerIntent, Callback mCallback) {
//这种方法详细实现了第三方sdk的接入, //recognizerIntent 携带了上层传入的參数
//mCallback 须要将第三方sdk的结果回调给该接口,是上层与第三方sdk的桥梁
} @Override
protected void onCancel(Callback listener)
{ //注销 }
@Override
public void onDestroy()
{ //销毁 super.onDestroy(); }
@Override
protected void onStopListening(Callback listener)
{ //暂停 }}


Callback的的接口有下面几个

public class Callback {
private final IRecognitionListener mListener; private Callback(IRecognitionListener listener) {
mListener = listener;
} public void beginningOfSpeech() throws RemoteException {
if (DBG) Log.d(TAG, "beginningOfSpeech");
mListener.onBeginningOfSpeech();
} public void bufferReceived(byte[] buffer) throws RemoteException {
mListener.onBufferReceived(buffer);
} public void error(int error) throws RemoteException {
Message.obtain(mHandler, MSG_RESET).sendToTarget();
mListener.onError(error);
} public void partialResults(Bundle partialResults) throws RemoteException {
mListener.onPartialResults(partialResults);
} public void readyForSpeech(Bundle params) throws RemoteException {
mListener.onReadyForSpeech(params);
} public void results(Bundle results) throws RemoteException {
Message.obtain(mHandler, MSG_RESET).sendToTarget();
mListener.onResults(results);
} public void rmsChanged(float rmsdB) throws RemoteException {
mListener.onRmsChanged(rmsdB);
}
}

之后会有一篇文章将一个实现了百度语音识别sdk的该服务代码展示出来。

上一篇:【网络通信】服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么?


下一篇:Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())[4]