1
我有一個通過語音啓動應用程序的命令:返回來自第三方的活動我的MainActivity通過VoiceRecognition
- 活性1(我的應用程序的其他活動)
- 活性2(我的應用程序的其他活動)
- 安裝在設備上的任何活動
我還可以通過將數字5(「五」)指向我的MainActivity從Activity1和Activity2返回。但是,如果我發起了第三方活動,這不起作用。我的主動性並沒有達到前景。也可以以編程方式觸發Back-Button(儘管如此,沒有運氣)。
有人可以給我一個提示嗎?
這裏是到目前爲止我的代碼:
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();
}
}
}
謝謝ashoke,我想我知道什麼是現在要做的......你能也許告訴我,如果結果後停靠onResults每次結束的功能startVoiceRecognitionActivity()是「OK」,以實現連續語音識別?有沒有負面的副作用呢?無論如何謝謝你! – treesoft 2014-11-03 21:33:38
@ maltonic42如果你想做[語音識別作爲後臺服務,請參閱此示例代碼](http://stackoverflow.com/a/18104872/1079716) – ashoke 2014-11-03 21:51:35
我試過這個:http://stackoverflow.com/問題/ 9997720 /如何註冊一個自定義語音識別服務onCreate VoiceServiceStarterActivity被稱爲如果我啓動服務,但onStartListening從未被稱爲..... – treesoft 2014-11-03 22:59:05