gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:18 25 4
gpt4 key购买 nike

我正在尝试创建一个包含单个 ImageView 的小部件,单击它会启动语音识别应用程序。我从未使用过小部件和挂起的 Intent ,所以我很困惑:如何为启动语音识别 Activity 创建挂起的 Intent ?

我试过这样的东西,但当然失败了:

   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);

最佳答案

我明白了!我需要两个包含在两个待定 Intent 中的常规 Intent ,如下所示:

// 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);

关于android - 调用语音识别应用程序的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4530472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com