Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何实现语音识别以启动应用程序?_Android_Service_Listener_Speech Recognition_Voice Recognition - Fatal编程技术网

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

Android 如何实现语音识别以启动应用程序?,android,service,listener,speech-recognition,voice-recognition,Android,Service,Listener,Speech Recognition,Voice Recognition,我是android开发的新手,我正试图找到一种使用语音识别启动我的应用程序的方法。我想在用户首次登录后启动一个在后台运行的识别服务。我希望服务能够持续运行,并允许用户使用关键字启动应用程序。我将允许用户在设置活动中停止服务。目前,我能够启动该服务并使其在后台运行,但我还无法确定如何在RecognitionService中调用onStartListening方法。非常感谢您对解决此问题的任何帮助 我的代码 public class VoiceRecognitionService extends

我是android开发的新手,我正试图找到一种使用语音识别启动我的应用程序的方法。我想在用户首次登录后启动一个在后台运行的识别服务。我希望服务能够持续运行,并允许用户使用关键字启动应用程序。我将允许用户在设置活动中停止服务。目前,我能够启动该服务并使其在后台运行,但我还无法确定如何在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();
        }
    }
}
公共类VoiceRecognitionService扩展了RecognitionService{
私人语音识别器m_EngineSR;
@凌驾
public void onCreate(){
super.onCreate();
Log.e(“SimpleVoiceService”、“服务已启动”);
}
@凌驾
公共空间{
super.ondestory();
Log.e(“SimpleVoiceService”、“服务已停止”);
}
@凌驾
受保护的void onCancel(回调侦听器){
m_EngineSR.cancel();
}
@凌驾

受保护的void onStartListening(Intent recognizer Intent,Callback listener){/虽然我还没有机会测试这个问题,但我认为答案在android开发者的博客文章中。
不过,这个实现使用了“Ok Google”(我不知道这是否会困扰您)

简而言之,您必须实现并将其添加到您的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>


这对我不起作用。我已对所有内容进行了三次检查,但无法将此搜索查询发送到我的应用程序。有人成功了吗?嗯……看起来该应用程序需要在Play Store上发布:-查找Jarek Wilkiewicz的评论。下一步将尝试此功能。
<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>