如何实施语音识别以启动应用程序?

时间:2014-10-27 21:21:45

标签: android service listener speech-recognition voice-recognition

我是Android开发的小伙子,我正试图想出一种使用语音识别启动我的应用程序的方法。我想在用户首次登录后开始在后台运行的RecognitionService。我希望服务能够持续运行并允许用户使用关键字启动应用程序。我将允许用户在设置活动中停止服务。目前,我能够启动服务并使其在后台运行,但我无法想象如何在RecognitionService中调用onStartListening方法。任何帮助解决这个问题都非常感谢。

我的代码

 public class VoiceRecognitionService extends RecognitionService {

private SpeechRecognizer m_EngineSR;

@Override
public void onCreate() {
    super.onCreate();
    Log.e("SimpleVoiceService", "Service started");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e("SimpleVoiceService", "Service stopped");
}

@Override
protected void onCancel(Callback listener) {
    m_EngineSR.cancel();
}

@Override
protected void onStartListening(Intent recognizerIntent, Callback listener) { //<--How Do I call this method?
    m_EngineSR.setRecognitionListener(new VoiceResultsListener(listener));
    m_EngineSR.startListening(recognizerIntent);
}

@Override
protected void onStopListening(Callback listener) {
    m_EngineSR.stopListening();
}


/**
 *
 */
private class VoiceResultsListener implements RecognitionListener {

    private Callback m_UserSpecifiedListener;

    /**
     *
     * @param userSpecifiedListener
     */
    public VoiceResultsListener(Callback userSpecifiedListener) {
        m_UserSpecifiedListener = userSpecifiedListener;
    }

    @Override
    public void onBeginningOfSpeech() {
        try {
            m_UserSpecifiedListener.beginningOfSpeech();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        try {
            m_UserSpecifiedListener.bufferReceived(buffer);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEndOfSpeech() {
        try {
            m_UserSpecifiedListener.endOfSpeech();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(int error) {
        try {
            m_UserSpecifiedListener.error(error);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEvent(int eventType, Bundle params) { ; }

    @Override
    public void onPartialResults(Bundle partialResults) {
        try {
            m_UserSpecifiedListener.partialResults(partialResults);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        try {
            m_UserSpecifiedListener.readyForSpeech(params);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResults(Bundle results) {
        try {
            m_UserSpecifiedListener.results(results);
            Log.e("SimpleVoiceService Results:", "Service stopped");

            ArrayList<String> result = results.getStringArrayList("results_recognition");

            Log.e("SimpleVoiceService Results:", result.get(0));
            if (result.isEmpty() == false) {
                for (int i = 0; i<(result.size()-1);i++) {
                    if(result.get(i).equals("shoot back")||result.get(i).equals("Launch App")){
                        Intent intent2 = new Intent("com.bmoney.testapp.MAIN_ACTIVITY");
                        startActivity(intent2);
                        break;

                    }
                }
            }



        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        try {
            m_UserSpecifiedListener.rmsChanged(rmsdB);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

}

清单

  <service android:name="com.bmoney.testapp.VoiceRecognitionService" android:exported="true" android:enabled="true">
        <intent-filter>
            <action android:name="android.speech.RecognitionService" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

1 个答案:

答案 0 :(得分:0)

虽然我没有机会对此进行测试,但我认为答案在于this android开发者博客文章。
这个实现虽然使用了“Ok Google”(我不知道这是否会让你烦恼)。

简而言之,您必须实施seachable activity并在AndroidManifest中添加此内容:

<activity android:name=".SearchableActivity">
    <intent-filter>
        <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>