2

This is a basic Speech to Text implementation. With a button press it records sound and displays words as text.

It worked flawlessly on android 8 and still does on a device that runs Oreo, but when i try to run it on a device that uses android 9 Pie the app crashes. The code is 100% the same i have not changed a thing.

Addition to the manifest file:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Java:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkPermission();

        final EditText editText = findViewById(R.id.editText);

        final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

        final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
            Locale.getDefault());

        mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

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

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                //getting all the matches
                ArrayList<String> matches = bundle
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                //displaying the first match
                if (matches != null)
                    editText.setText(matches.get(0));
            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        });

        findViewById(R.id.button).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_UP:
                        mSpeechRecognizer.stopListening();
                        editText.setHint("You will see input here");
                        break;

                    case MotionEvent.ACTION_DOWN:
                        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
                        editText.setText("");
                        editText.setHint("Listening...");
                        break;
                }
                return false;
            }
        });
    }


    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                    Uri.parse("package:" + getPackageName()));
                startActivity(intent);
                finish();
            }
        }
    }
}

I know that posting the entire code text is not common practice but i have no other way of letting you know what could be missing that is required on Android 9.

RUN LOGS:

E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.SecurityException: Not allowed to bind to service Intent { act=android.speech.RecognitionService cmp=com.samsung.android.bixby.agent/.mainui.voiceinteraction.MainVoiceInteractionService }
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1766)
    at android.app.ContextImpl.bindService(ContextImpl.java:1701)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:711)
    at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:287)
    at tk.gandriks.speechtest.MainActivity$2.onTouch(MainActivity.java:107)
    at android.view.View.dispatchTouchEvent(View.java:13467)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1869)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
    at android.view.View.dispatchPointerEvent(View.java:13719)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6111)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5889)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5516)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5573)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8400)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8333)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8286)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8515)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:326)
    at android.os.Looper.loop(Looper.java:181)
    at android.app.ActivityThread.main(ActivityThread.java:7058)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: tk.gandriks.speechtest, PID: 9824
    java.lang.SecurityException: Not allowed to bind to service Intent { act=android.speech.RecognitionService cmp=com.samsung.android.bixby.agent/.mainui.voiceinteraction.MainVoiceInteractionService }
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1766)
    at android.app.ContextImpl.bindService(ContextImpl.java:1701)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:711)
    at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:287)
    at tk.gandriks.speechtest.MainActivity$2.onTouch(MainActivity.java:107)
    at android.view.View.dispatchTouchEvent(View.java:13467)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1869)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
    at android.view.View.dispatchPointerEvent(View.java:13719)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6111)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5889)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5516)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5573)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8400)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8333)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8286)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8515)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:326)
    at android.os.Looper.loop(Looper.java:181)
    at android.app.ActivityThread.main(ActivityThread.java:7058)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 9824 SIG: 9
Application terminated.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Greg
  • 357
  • 1
  • 11
  • [Not allowed to bind to service Intent ] Different android versions require different ways of asking for permissions. – ivan May 31 '19 at 16:44
  • Thought as much... It always has to be a permissions thing... – Greg May 31 '19 at 16:46
  • 1
    Actually i just checked around and the reason might not be that, take a look at: https://stackoverflow.com/questions/25250761/speech-recognizer-on-htc-one-m7 – ivan May 31 '19 at 16:47
  • I tried the enabling the speech option as google's default and it didn't help. App keeps crashing. – Greg May 31 '19 at 16:52
  • Is the logcat error the same after enabling it? – ivan May 31 '19 at 16:54
  • Comparing the logfiles, some code numbers have changed but the log is mostly the same. Events are the same. – Greg May 31 '19 at 17:00
  • OK this is strange... Turns out it DOES work on android 9, but not in all devices. It works on a Pocophone F1 running Pie, but not on a Galaxy A50 also running Pie. Why? How is this even possible? – Greg May 31 '19 at 18:08
  • Its not that weird, thats why i linked the stack question, that one had the same problem with an specific device.(HTC ones to be exact) – ivan May 31 '19 at 18:27

0 Answers0