Android 7.0 上的 SpeechRecognizer

SpeechRecognizer on Android 7.0

我实现了 SpeechRecognizer,它在 Android 6 上运行良好,但在 android 7.0 上运行不正常,我不知道为什么。

如果我尝试在 startActivityforResult() 上使用识别器,它工作得很好,但如果我使用 createListener 方法,没有任何反应,即使我尝试输出所有结果。

这是我的代码:

class listener implements RecognitionListener
{
    String TAG = "AFFICHAGE";
    public void onReadyForSpeech(Bundle params)
    {
        Toast.makeText(MainActivity.this,"READY TO LISTEN", Toast.LENGTH_LONG).show();
    }
    public void onBeginningOfSpeech()
    {

    }
    public void onRmsChanged(float rmsdB)
    {

    }
    public void onBufferReceived(byte[] buffer)
    {
        Log.d(TAG, "onBufferReceived");
    }
    public void onEndOfSpeech()
    {
        Log.d(TAG, "onEndofSpeech");
    }
    public void onError(int error)
    {
        switch (error){
            case SpeechRecognizer.ERROR_AUDIO:
                Toast.makeText(MainActivity.this, "ERROR AUDIO",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_CLIENT:
                Toast.makeText(MainActivity.this, "ERROR CLIENT",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                Toast.makeText(MainActivity.this, "ERROR PERMISSION",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_NETWORK:
                Toast.makeText(MainActivity.this, "ERROR INTERNET",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                Toast.makeText(MainActivity.this, "ERROR TIMEOUT",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_NO_MATCH:
                Toast.makeText(MainActivity.this, "ERROR NO MATCH",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                Toast.makeText(MainActivity.this, "ERROR BUSY",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_SERVER:
                Toast.makeText(MainActivity.this, "ERROR SERVER",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                Toast.makeText(MainActivity.this, "ERROR TIMEOUT",Toast.LENGTH_LONG).show();

                break;

            default:

                break;
        }
    }
    public void onResults(Bundle results)
    {
        String str = new String();
        Log.d(TAG, "onResults " + results);
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (int i = 0; i < data.size(); i++)
        {
            Log.d(TAG, "result " + data.get(i));
            str += data.get(i);
        }
        txtSpeechInput.setText("results: "+String.valueOf(data.size()));
    }
    public void onPartialResults(Bundle partialResults)
    {
        Log.d(TAG, "onPartialResults");
    }
    public void onEvent(int eventType, Bundle params)
    {
        Log.d(TAG, "onEvent " + eventType);
    }
}

我在这里称呼它:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
    btn = (Button) findViewById(R.id.button);
    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new listener());
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            //intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

            //intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
            sr.startListening(intent);
            Log.i("111111","11111111");
        }
    });
}

感谢回答。

我找到了一个解决方案,可以像这样初始化 google 图书馆:SpeechRecognizer.createSpeechRecognizer(getApplication(), ComponentName.unflattenFromString("com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService")); 它的工作,所以我现在不知道它是否是一个好的解决方案。