2

i have an app which launches by voice commands:

  1. Activity1 (another Activity of my app)
  2. Activity2 (another Activity of my app)
  3. Any Activity installed on device

i can also return from Activity1 and Activity2 by saying the number 5 ("five") to my MainActivity. But this doesnt work if i have launched a third-party activity. My MainActivity just doesnt come to foreground. It would also be ok to trigger Back-Button programmatically (had no luck with that either though).

Can someone give me a hint?

here is my code so far:

 public class MainActivity extends Activity
    {
        public static ListView      wordsList;

        private SpeechRecognizer    mSpeechRecognizer;
        private Intent              mSpeechRecognizerIntent;
        public static Context       mContext    = null;
        public Button               mButton1    = null;
        public Button               mButton2    = null;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.voice_recog);

            mContext = this;

            ToggleButton speakButton = (ToggleButton) findViewById(R.id.speakButton);

            mButton1 = (Button) findViewById(R.id.button1);
            mButton2 = (Button) findViewById(R.id.button2);

            wordsList = (ListView) findViewById(R.id.list);

            // Disable button if no recognition service is present
            PackageManager pm = getPackageManager();
            List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
            if (activities.size() == 0)
            {
                speakButton.setText("Recognizer not present");
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        public void speakButtonClicked(View v)
        {
            ToggleButton btn = (ToggleButton) v;
            if (btn.isChecked())
                startVoiceRecognitionActivity();
            else
                mSpeechRecognizer.stopListening();
        }

        public void button1Clicked(View v)
        {

            Intent i = new Intent(MainActivity.this, Activity1.class);
            this.startActivity(i);
        }

        public void button2Clicked(View v)
        {

            Intent i = new Intent(MainActivity.this, Activity2.class);
            this.startActivity(i);
        }

        private void startVoiceRecognitionActivity()
        {
            mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
            SpeechRecognitionListener listener = new SpeechRecognitionListener();
            mSpeechRecognizer.setRecognitionListener(listener);

            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.getPackageName());

            mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
        }

        public class SpeechRecognitionListener extends Activity implements RecognitionListener
        {
            @Override
            public void onResults(Bundle results)
            {
                ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                wordsList.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, matches));

                PackageManager packageManager = mContext.getPackageManager();
                Intent main_intent = new Intent(Intent.ACTION_MAIN, null);
                main_intent.addCategory(Intent.CATEGORY_LAUNCHER);

                List<ResolveInfo> launchables = packageManager.queryIntentActivities(main_intent, 0);

                for (int i = 0; i < matches.size(); i++)
                {

                    String match = matches.get(i).toUpperCase();
                    for (int j = 0; j < launchables.size(); j++)
                    {

                        String activity = ((String) launchables.get(j).activityInfo.loadLabel(packageManager)).toUpperCase();

                        if (match.equals(activity))
                        {
                            String name = launchables.get(j).activityInfo.name;
                            String packageName = launchables.get(j).activityInfo.packageName;

                            ComponentName componentName = new ComponentName(packageName, name);
                            Intent intent = new Intent(Intent.ACTION_MAIN);

                            intent.addCategory(Intent.CATEGORY_LAUNCHER);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                            intent.setComponent(componentName);
                            int tmp = 0;
                            mContext.startActivity(intent);
                            break;
                        }
                    }

                    if (match.equals("1"))
                    {
                        mButton1.performClick();
                        break;
                    }

                    if (match.equals("2"))
                    {
                        mButton2.performClick();
                        break;
                    }

                    if (match.equals("5"))
                    {

                        Intent intent = new Intent(MainActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        mContext.startActivity(intent);
                        break;
                    }

                }
                startVoiceRecognitionActivity();
            }
        }
    }
treesoft
  • 295
  • 2
  • 14

1 Answers1

2
  • once you kick off another activity, it has the full control. You will not be able to intercept user input (key or voice).

  • One option is to create a broadcast receiver and listen to back action, or create a background service listen to voice.

  • When you detect a back key was pressed, or voice action, kick off your main activity from the broadcast receiver or the background service.

  • You may consider using SingleTask launch mode for your Main activity so it goes back to the existing Main activity.

ashoke
  • 6,441
  • 2
  • 26
  • 25
  • thank you ashoke, i think i know what is to do now...could you maybe tell me if calling the function startVoiceRecognitionActivity() at the end of onResults everytime after a result is "ok" to realize continous voice-recognition? are there any negative sideeffects to this? thank you in any case! – treesoft Nov 03 '14 at 21:33
  • @maltonic42 if you want to do [voice recognition as background service, see this for an example code](http://stackoverflow.com/a/18104872/1079716) – ashoke Nov 03 '14 at 21:51
  • i tried this: http://stackoverflow.com/questions/9997720/how-to-register-a-custom-speech-recognition-service onCreate of VoiceServiceStarterActivity is called if i start service, but onStartListening is never called..... – treesoft Nov 03 '14 at 22:59
  • @maltonic42 can you see any logcat messages. Make sure manifest has been properly updated. please enter a separate question for this, with details/log of the problem. – ashoke Nov 04 '14 at 00:14
  • i am one step further (also understanding it a lil better). If you look at the code i linked: at calling function "onStartListening" m_EngineSR is null which causes my app to crash. Do you know what might be the cause for this? – treesoft Nov 04 '14 at 00:36
  • @maltonic42 in that code snippet `m_EngineSR` is not being created. Use [one of these factory methods to create/initialize](http://developer.android.com/reference/android/speech/SpeechRecognizer.html#createSpeechRecognizer(android.content.Context,android.content.ComponentName)) the variable `m_EngineSR` – ashoke Nov 04 '14 at 00:44
  • thank you again, i did see that too shortly after i posted last time. Ive created another question (regarding this) which describes better what i`m currently stuck with: https://stackoverflow.com/questions/26744520/not-getting-results-from-custom-voice-recognitionservice – treesoft Nov 04 '14 at 20:52