2

i want to make an application that i can run it over voice like siri or google assistant, so in order to make it i have to implement the code in a background or foreground service which will run speech recognizer so is this possible,because of the limitation of the working services on android Oreo and higher this make me stuck in the middle of nowhere and i'm not so professional so i could figure this out myself

i have tried to make a foreground service with IntentService so that it could make the work on the background without freezing the UI and the Speech recognizer didn't work

package com.example.intentservice;

import android.app.IntentService;
import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.List;
import java.util.Locale;


import static com.example.intentservice.App.CHANNEL_ID;

public class ExampleService extends IntentService {
    public static final String TAG = "ExampleService";
    private PowerManager.WakeLock wakeLock;
    private TextToSpeech textToSpeech;
    private SpeechRecognizer recognizer;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate");


        initRec();
        startListening();

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ExampleService:wakelock");
        wakeLock.acquire();
        Log.e(TAG, "wakelock acquired");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification notification = new Notification.Builder(this, CHANNEL_ID)
                    .setContentTitle("noti")
                    .setContentText("running")
                    .build();
            startForeground(1, notification);
        }
    }

    private void startListening() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
        recognizer.startListening(intent);
    }

    private void initRec() {
        if (SpeechRecognizer.isRecognitionAvailable(this)) {
            recognizer = SpeechRecognizer.createSpeechRecognizer(this);
            recognizer.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) {
                    List<String> res = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                    Toast.makeText(ExampleService.this, res.get(0), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPartialResults(Bundle bundle) {

                }

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

                }
            });
        }
    }





    public ExampleService() {
        super("ExampleService");
//        to create service again
        setIntentRedelivery(true);
    }


// this just to test the code
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.e(TAG, "onHandleIntent");
        String string = intent.getStringExtra("key");
        for (int i = 0; i < 10; i++) {
            Log.e(TAG, string + "-" + i);
//            Toast.makeText(this, "i", Toast.LENGTH_SHORT).show();
            SystemClock.sleep(1000);

        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
        wakeLock.release();
        Log.e(TAG, "wakelock realised");
    }
}
  • Possible duplicate of [Android Speech Recognition Continuous Service](https://stackoverflow.com/questions/18039429/android-speech-recognition-continuous-service) – Nikolay Shmyrev Nov 20 '19 at 11:32
  • this is for android 4 and i need the app to run on higher api levels, and by the way a foreground service won't run on the main thread and speech recognizer works only on the main thread – Mohammad Sorour Nov 22 '19 at 17:45
  • Hi, I'm trying to do the same thing. Do you solve this question in some manner? Tnx!!! – Hammeronthenet Oct 25 '20 at 14:30
  • It cannot be done because the service need to run on the main thread you will have to access the accelerometer sensor and when it senses a a change you can open your app – Mohammad Sorour Nov 20 '20 at 15:23

0 Answers0