Android 识别器悬而未决流的工作示例

Android 识别器悬而未决流的工作示例,android,wear-os,android-pendingintent,Android,Wear Os,Android Pendingintent,我使用以下代码设置启动语音识别器,并使用PendingEvent启动另一个活动: Intent voiceActivityIntent = new Intent (MainActivity.this, VoiceActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity (MainActivity.this, 0, voiceActivityIntent, PendingIntent.FLAG_ONE

我使用以下代码设置启动语音识别器,并使用PendingEvent启动另一个活动:

Intent voiceActivityIntent = new Intent (MainActivity.this, VoiceActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity (MainActivity.this, 0, 
    voiceActivityIntent, PendingIntent.FLAG_ONE_SHOT);

Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    .putExtra (RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    .putExtra (RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
文档说明startActivity不能用于识别器Intent.ACTION\u Recognition\u SPEECH,使用startActivityForResult只会将结果返回到不需要的当前活动(MainActivity)

我试过:

pendingIntent.send ();
但这只是将我带到VoiceActivity.class,而不执行识别器


我目前正在Android Wear Round API 21模拟器上进行测试。

只需在代码中添加以下内容:

    // this intent wraps voice recognition intent 
    PendingIntent pendingIntVoice = PendingIntent.getActivity(context, 0, intent, 0);
    pendingIntVoice.send();

换句话说,使用
pendingent.send()
你不是在调用语音识别器Intent,而是应该在语音识别结束时自动调用的Intent(事实上,你用
putExtra(RecognizerIntent.EXTRA_RESULTS_pendingent,pendingent)设置它)
行)

我想要类似的东西,下面是我想到的,它可以工作。这是一个服务的内部:

//IntentHandlerService extends IntentService
//The intent I want sent back to me after voice recognition is complete...
Intent inputTextIntent = new Intent(this, IntentHandlerService.class);
//...gets wrapped in this PendingIntent...
PendingIntent pendingIntent = PendingIntent.getService(this, 0, inputTextIntent, PendingIntent.FLAG_ONE_SHOT);
//...and added to the intent aimed for the recognizer...
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent);
//...which is the one you start.
startActivity(intent);