Bill Harper Bill Harper - 6 months ago 157
Android Question

Insufficient permissions for SpeechRecognizer despite setting it in the manifest

I'm just trying to create a dummy app for speech recognition on clicking a button (with no pop-ups or anything).

My Android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="billobob.org.speechtest">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

</manifest>


And the fragment housing what's actually going on:

package billobob.org.speechtest;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Simple app for recognizing speech
*/
public class MainActivityFragment extends Fragment {

protected static final int RESULT_SPEECH = 1234;

private TextView mSpeechTextView1;
private TextView mSpeechTextView2;
private Button mSpeechButton;
private String speechString;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
boolean mIsListening = false;

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
mSpeechTextView1 = (TextView) view.findViewById(R.id.textView1);
mSpeechTextView2 = (TextView) view.findViewById(R.id.textView2);
mSpeechButton = (Button) view.findViewById(R.id.speechButton);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this.getContext());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getActivity().getPackageName());


SpeechRecognitionListener listener = new SpeechRecognitionListener();
mSpeechRecognizer.setRecognitionListener(listener);

mSpeechButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mIsListening)
{
Log.d("UUXX", "clicked");
mIsListening = true;
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
}
});
return view;
}

@Override
public void onDestroyView() {
if (mSpeechRecognizer != null)
{
mSpeechRecognizer.stopListening();
mSpeechRecognizer.cancel();
mSpeechRecognizer.destroy();
}
super.onDestroyView();
}

protected class SpeechRecognitionListener implements RecognitionListener {
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("UUSP", "in read");
}

@Override
public void onBeginningOfSpeech() {
Log.d("UUSP", "begin!");
}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {
Log.d("UUSP", "end");
}

@Override
public void onError(int error) {

}

@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d("UUSP", matches != null ? matches.get(0) : null);
mIsListening = false;
mSpeechTextView1.setText(matches.get(0));
}

@Override
public void onPartialResults(Bundle partialResults) {
Log.d("UUSP", "partial...");
}

@Override
public void onEvent(int eventType, Bundle params) {
Log.d("UUSP", "event?");
}
}
}


The button registers the click, but nothing else happens. I noticed in the non-application log that the error:

05-20 20:56:32.022 18200-19108/? E/RecognitionService: call for recognition service without RECORD_AUDIO permissions


Always occurs, despite the fact that I ostensibly have permissions set in the manifest. I'm testing it on a 6P with Android Studio 6.1. Any help would be much appreciated!

Answer

For API 23+ (Android 6.0) it is not enough to just add permissions to the manifest. You need to request permissions at run time.

See the developer documents for more information: https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous https://developer.android.com/training/permissions/requesting.html

You can verify this is the issue by changing your gradle targetSdkVersion back to 21. It will then use the old permission model on devices running API 23+