在模拟器和真实设备上都使用语音识别时,尽管清单明显,但“未连接网络”

时间:2019-03-15 17:48:58

标签: android android-emulator android-manifest voice-recognition

我正在尝试使用Android Studio进行语音识别。

我正在使用this website中的教程。

我知道语音识别的一些典型问题,所以我再次检查了这一点:

  1. 我的清单文件包含其他权限。
  2. 网络可在模拟器上运行。
  3. 测试语音识别器是否可用-已通过模​​拟器。

我已经在模拟器(Nexus 5X API 28 x86)上对其进行了测试。

语音识别显示“网络未连接”(这是不正确的):

enter image description here

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mei.chat">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.example.mei.chat.SimpleVoiceRecognition"
        android:label="@string/title_activity_simple_voice_recognition"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity android:name="com.example.mei.chat.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我的MainActivity文件:

package com.example.mei.chat;

import android.app.Activity;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;

public class MainActivity extends Activity {

private final int REQUEST_SPEECH_RECOGNIZER = 3000;
//private TextView mTextView;
private final String mQuestion = "Which company is the largest online retailer on the planet?";
private String mAnswer = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
   // mTextView = (TextView)findViewById(R.id.tvstt);

    startSpeechRecognizer();
}

private void startSpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, mQuestion);
    startActivityForResult(intent, REQUEST_SPEECH_RECOGNIZER);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_SPEECH_RECOGNIZER) {
        if (resultCode == RESULT_OK) {
            List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mAnswer = results.get(0);
            ...
        }
    }
}
}

运行日志看起来很干净(唯一的“错误”似乎是有关GPU加速的信息,我认为它们是无害的-“ E / eglCodecCommon”):

03/15 18:44:12: Launching app
$ adb shell am start -n "com.example.sergeherkul.hellotoast/com.example.mei.chat.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 18254 on device Nexus_5X_API_28_x86 [emulator-5554]
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/rkul.hellotoas: Not late-enabling -Xcheck:jni (already on)
W/rkul.hellotoas: Unexpected CPU variant for X86 using defaults: x86
I/rkul.hellotoas: The ClassLoaderContext is a special shared library.
W/rkul.hellotoas: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10086(com.example.sergeherkul.hellotoast) identical 10 lines
W/rkul.hellotoas: JIT profile information will not be recorded: profile file does not exits.
I/InstantRun: starting instant run server: is main process
D/OpenGLRenderer: Skia GL Pipeline
D/: HostConnection::get() New Host Connection established 0xe0894b80, tid 18280
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xe40de700: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xe40de700: ver 3 1 (tinfo 0xe8047b00)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
D/EGL_emulation: eglMakeCurrent: 0xe40de700: ver 3 1 (tinfo 0xe8047b00)

要测试语音识别器在模拟器上是否可用,我使用了以下代码:

PackageManager pm = getPackageManager();
List activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
    Toast.makeText(this, "Voice recognizer is not available in your device", Toast.LENGTH_SHORT).show();
}else{
    Toast.makeText(this, "OK. Voice recognizer is available.", Toast.LENGTH_SHORT).show();
}

0 个答案:

没有答案