portfoliobuilder
portfoliobuilder

Reputation: 7856

How to keep voice recognition while device is asleep?

I was wondering, how can I keep the device listening for voice commands with voice recognition while the device is asleep? The idea I have is that I would like the device to respond to my voice, even if I have the screen locked or the screen has timed out.

Is this possible? I have tried using this as a service and an interface and it stops listening once the screen locks. Can I receive any help with this? This is my class.

public class VoiceEngineService extends Activity {

    private boolean isSpeakingDone = false; // default setting
    private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);

    private AudioManager mAudioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.wait_for_speech);

            // mute beep sound 
            mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);

            sr.setRecognitionListener(new listener());
            Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // LANGUAGE_MODEL_WEB_SEARCH
            i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
                    .getClass().getName());
            i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
            i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
            i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5500);
            sr.startListening(i);
    }

    class listener implements RecognitionListener {

        @Override
        public void onReadyForSpeech(Bundle params) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onBeginningOfSpeech() {
            // TODO Auto-generated method stub
        }

        @Override
        public void onRmsChanged(float rmsdB) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onEndOfSpeech() {
            // TODO Auto-generated method stub
        }

        @Override
        public void onError(int error) {
            // TODO Auto-generated method stub
            if (SharedPref.getVoiceController() == false) {
                sr.cancel();
                Intent i = new Intent();
                sr.startListening(i);
            } else {
                sr.stopListening();
                sr.destroy();
                finish();
            }

        }

        @Override
        public void onResults(Bundle results) {
            // TODO Auto-generated method stub
            isSpeakingDone = true;
            ArrayList<String> mDataList = results
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            Intent i = new Intent();
            i.putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, mDataList);
            setResult(RESULT_OK, i);
            finish();
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            // TODO Auto-generated method stub
        }

    } // end listener class

    @Override
    protected void onPause() {
        if ((isSpeakingDone == false)) {
            finish();
        }
        super.onPause();
    }

    @Override
    protected void onStop() {
        // when speaking is true finish() has already been called
        if (isSpeakingDone == false) {
            finish();
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        sr.stopListening();
        sr.destroy();

        super.onDestroy();
    }
}

Upvotes: 0

Views: 208

Answers (1)

Dumbo
Dumbo

Reputation: 14112

You have to implement your voice listener in a service. Create a class that extends 'Service' and make up some logic to take care of recording.

If you tried already the service, then it might be that you tried to redirect commands to an activity which most likely has been stopped by Android OS. Generally when talking about doing stuff when phone is in lock mode, you only can hope to accomplish tasks in one or more services coupled togethe.

When you are in Activity, of course wen the activity goes out of scope it will be shut down by Android OS. but services can still run in background unless shut dow mm explicitly by your own code or in rare cases that Android will recognize that it needs memory and processor power for other tasks.

Upvotes: 2

Related Questions