为什么 SpeechRecognizer 说我没有足够的权限,即使我已经指定了 RECORD_AUDIO 权限?
Why does SpeechRecognizer say that I have insufficient permissions even when I've specified the RECORD_AUDIO permission?
我正在尝试在我的应用程序中进行语音识别。我收到一个权限错误,提示我需要 RECORD_AUDIO 权限,但我已在我的清单中指定了该权限。我在 Android L 上,也在为那个 API 版本构建(没有更低)。这是确切的错误:
03-09 22:43:58.996 1555-1576/com.google.android.googlequicksearchbox:search E/RecognitionService﹕ call for recognition service without RECORD_AUDIO permissions
怎么了?这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andrewstromme.myapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
来自 MainActivity.java:
public class MainActivity extends Activity implements RecognitionListener {
private SpeechRecognizer mRecognizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
mRecognizer.setRecognitionListener(this);
}
public void listenForSpeech() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "say something");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
mRecognizer.startListening(intent);
}
void showToastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
showToastMessage("start of speech");
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
if (error == SpeechRecognizer.ERROR_AUDIO) {
showToastMessage("audio recording error");
} else if (error == SpeechRecognizer.ERROR_CLIENT) {
showToastMessage("client side error");
} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
showToastMessage("insufficient permissions");
} else if (error == SpeechRecognizer.ERROR_NETWORK) {
showToastMessage("network error");
} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
showToastMessage("network timeout");
} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
showToastMessage("no speech match");
} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
showToastMessage("recognizer busy");
} else if (error == SpeechRecognizer.ERROR_SERVER) {
showToastMessage("server error");
} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
showToastMessage("no speech input");
} else {
showToastMessage("unknown error");
}
}
@Override
public void onResults(Bundle results) {
String result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0);
TextView t = (TextView)findViewById(R.id.spokenText);
t.setText(result);
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
}
哦,我觉得很傻。我的 <uses-permission ...>
标签需要在我的 <application></application>
标签之外。
我正在尝试在我的应用程序中进行语音识别。我收到一个权限错误,提示我需要 RECORD_AUDIO 权限,但我已在我的清单中指定了该权限。我在 Android L 上,也在为那个 API 版本构建(没有更低)。这是确切的错误:
03-09 22:43:58.996 1555-1576/com.google.android.googlequicksearchbox:search E/RecognitionService﹕ call for recognition service without RECORD_AUDIO permissions
怎么了?这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andrewstromme.myapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
来自 MainActivity.java:
public class MainActivity extends Activity implements RecognitionListener {
private SpeechRecognizer mRecognizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
mRecognizer.setRecognitionListener(this);
}
public void listenForSpeech() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "say something");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
mRecognizer.startListening(intent);
}
void showToastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
showToastMessage("start of speech");
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
if (error == SpeechRecognizer.ERROR_AUDIO) {
showToastMessage("audio recording error");
} else if (error == SpeechRecognizer.ERROR_CLIENT) {
showToastMessage("client side error");
} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
showToastMessage("insufficient permissions");
} else if (error == SpeechRecognizer.ERROR_NETWORK) {
showToastMessage("network error");
} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
showToastMessage("network timeout");
} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
showToastMessage("no speech match");
} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
showToastMessage("recognizer busy");
} else if (error == SpeechRecognizer.ERROR_SERVER) {
showToastMessage("server error");
} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
showToastMessage("no speech input");
} else {
showToastMessage("unknown error");
}
}
@Override
public void onResults(Bundle results) {
String result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0);
TextView t = (TextView)findViewById(R.id.spokenText);
t.setText(result);
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
}
哦,我觉得很傻。我的 <uses-permission ...>
标签需要在我的 <application></application>
标签之外。