就像this问题(接受的答案)一样,我是'm trying to launch voice recognition from one of my app'的小部件 . 我成功地设置了打开对话框,在Widget的 onUpdate()
方法中使用此代码请求语音输入:
// this intent points to activity that should handle results, doesn't work
Intent activityIntent = new Intent(SoulissApp.getAppContext(), WrapperActivity.class );
//doesn't work as well
//activityIntent.setComponent(new ComponentName("it.angelic.soulissclient", "it.angelic.soulissclient.WrapperActivity"));
// this intent wraps results activity intent
PendingIntent resultsPendingIntent = PendingIntent.getActivity(SoulissApp.getAppContext(), 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);
Bundle fakeBun = new Bundle();
fakeBun.putChar("fake", 'f');
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, fakeBun);
// this intent wraps voice recognition intent, works
PendingIntent pendingInt = PendingIntent.getActivity(context, 0, voiceIntent, 0);
updateViews.setOnClickPendingIntent(R.id.button1, pendingInt);
语音识别有效,但 at the end of the recognition process, my resultsPendingIntent is not called. Why?
从sys日志中,我读到了这个:
...I/ActivityManager﹕ START u0 {cmp=it.angelic.soulissclient/.SoulissWidgetVoice (has extras)} from uid 10152 on display 0......
虽然我期待类似的东西:
...I/ActivityManager﹕ START u0 {cmp=it.angelic.soulissclient/.WrapperActivity(has extras)}...
因为 .WrapperActivity
正在等待Intent,而 SoulissWidgetVoice
是小部件类 . 请注意, WrapperActivity
本身可以从其他活动正确启动,它是 Theme.NoDisplay
基本活动:
<activity
android:name=".WrapperActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoDisplay"/>
1 回答
这是完全正常的,它基于Android SDK中的ListView . 它不是特别适用于小部件,但我确信您可以修改它以使其适用于小部件 .
创建一个名为SearchActivity的活动:
将活动添加到AndroidManifest.xml
在您的自定义窗口小部件/视图中: