调用语音识别应用程序的小部件

zor*_*b76 13 android speech-recognition widget

我正在尝试创建一个包含单个ImageView的窗口小部件,当单击它时,它启动语音识别应用程序.我从来没有使用过小部件和待定的意图,所以我很困惑:如何创建一个未决的意图来启动语音识别活动?

我尝试过类似的东西,当然,它失败了:

   Intent intent = new Intent();
   Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
   voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
     RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
   voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT,
     "Speech recognition demo");
   voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, voiceIntent);
   PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
     intent, 0);
   RemoteViews views = new RemoteViews(context.getPackageName(),
     R.layout.main);
   views.setOnClickPendingIntent(R.id.button, pendingIntent);

zor*_*b76 9

我知道了!我需要两个常规意图包含在两个待处理的意图中,如下所示:

// this intent points to activity that should handle results
Intent activityIntent = new Intent(context, ResultsActivity.class);
// this intent wraps results activity intent
PendingIntent resultsPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);

// this intent calls the speech recognition
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, resultsPendingIntent);

// this intent wraps voice recognition intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, voiceIntent, 0);
rv.setOnClickPendingIntent(R.id.btn, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

  • 你可以发布ResultsActivity的代码吗?我正在尝试实现类似的东西...... (2认同)