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

时间:2010-12-25 14:07:42

标签: android widget speech-recognition

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

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分:4)

我也遇到同样的问题 很抱歉,我没有足够的声誉发表评论。

无需使用透明活动来发送识别意图 喜欢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 !!

获得结果

答案 2 :(得分:1)

我想创建像谷歌一样的小部件。我尝试过zorglub76解决方案,但是我无法获得结果......

我通过创建一个虚拟透明活动解决了这个问题,该活动可以端到端地处理语音识别。

它的工作方式如下:Widget-&gt; VoiceRecognitionStarterActivity-&gt; RecognizerIntent-&gt; 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);
    }

}

要使活动透明,请参阅this发布

答案 3 :(得分:0)

这是完全正常的,它基于Android SDK中的ListView小部件。它并不特别适用于小部件,但我确信您可以对其进行修改,使其适用于小部件。

创建一个名为SearchActivity的活动:

CALayer

将活动添加到AndroidManifest.xml

// CustomSearch (View) & ISearch (Interface) are objects that I created and are irrelevant
public class SearchActivity extends AppCompatActivity implements ISearch
{
    // Variables
    private CustomSearch mSearchView;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        mSearchView = (CustomSearch)findViewById(R.id.search);
        mSearchView.setPendingComponentName(getComponentName());
        mSearchView.setSearchListener(this);
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.i("SEARCH >", "You said: " + query);
        }
    }
}

在自定义窗口小部件/视图中:

<activity
    android:name=".activities.SearchActivity"
    android:label="@string/app_name"
    android:theme="@style/CustomTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
    </intent-filter>
</activity>