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

我正在尝试创建一个包含单个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);

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

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

我也遇到同样的问题。
对不起,我没有足够的声望发表评论。

没有必要使用透明的活动来发送识别意图。
就像zorglub76的回答一样

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

识别结果将只是在的额外resultingPendingIntent
所以你需要做的是:

ResultsActivity.onCreate()

ArrayList<String> voiceResults = this.getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);

注意NullPointerException ,你会从ArrayList中得到结果!


我想创建谷歌像小部件。 我尝试了zorglub76解决方案,但我无法得到结果的声音...

我通过创建一个虚拟transparrent活动来处理语音识别端到端来解决这个问题。

它的工作方式如下:Widget-> VoiceRecognitionStarterActivity-> RecognizerIntent-> VoiceRecognitionStarterActivity.onActivityResult。

我的小部件类:

public class MyWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent activityIntent = new Intent(context, VoiceRecognitionStarterActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.mic_image, pendingIntent);

        activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(context.getString(R.string.search_url)));
        pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.search_box_image, pendingIntent);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);

    }
    }
}

我的透明活动:

   public class VoiceRecognitionStarterActivity extends Activity
{
    private static final String TAG = "VoiceRecognitionStarterActivity";
    private int SPEECH_REQUEST_CODE = 1;

    @Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sendRecognizeIntent();
}

private void sendRecognizeIntent()
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to search");
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == SPEECH_REQUEST_CODE)
    {
        if (resultCode == RESULT_OK) {
            Log.d(TAG, "result ok");
            Intent searchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.search_url)));
            startActivity(searchIntent);
            finish();   
         } else {
            Log.d(TAG, "result NOT ok");
            finish();
        }

    }

    super.onActivityResult(requestCode, resultCode, data);
    }

}

要使活动透明,请参阅此帖子

链接地址: http://www.djcxy.com/p/50621.html

上一篇: Widget that calls speech recognition app

下一篇: TextView not getting updated in android app widgets