开发者

Widget that calls speech recognition app

I'm trying to create a widget that contains a single ImageView which, when clicked, starts speech recognition application. I've never worked with widgets and pending intents, so I'm confused: how to create a pending intent for starting speech recognition activity?

I tried with something like this, but it, of course, fails:

   Intent intent = new Intent();
   Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
   voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
     Rec开发者_如何学编程ognizerIntent.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);


I got it! I needed two regular intents wrapped in two pending intents, like this:

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


I encounter the same problem, too.
Sorry that I don't have enough reputation to comment.

There's no need to use a transparent activity to send a recognition intent.
Like the answer of 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);

The recognition result will just be in the extra of the resultingPendingIntent
So all you need to do is:

In ResultsActivity.onCreate()

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

Be care of the NullPointerException, and you'll get the result from the ArrayList!!


I wanted to create google like widget. I tried zorglub76 solution, but I wasn't able to get voice the result...

I solved it by creating a dummy transparrent activity that handles the voice recognition end-to-end.

It work as follows: Widget->VoiceRecognitionStarterActivity->RecognizerIntent->VoiceRecognitionStarterActivity.onActivityResult.

My widget class:

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

    }
    }
}

My transparrent activity:

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

}

To make the activity transparrent, see this post


This is fully functional, and it's based off the ListView widget in the Android SDK. It's not particularly for a widget, but I'm sure you can modify it so that it works for a widget.

Create an activity called SearchActivity:

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

Add activity to the AndroidManifest.xml

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

In your custom Widget/View:

buttonVoice.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v)
    {
        // Get activity from either SearchableInfo or ComponentName
        ComponentName searchActivity = mComponentName;

        // Wrap component in intent
        Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
        queryIntent.setComponent(searchActivity);

        // Wrap query intent in pending intent
        PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent, PendingIntent.FLAG_ONE_SHOT);

        // Create bundle now because if we wrap it in pending intent, it becomes immutable
        Bundle queryExtras = new Bundle();

        // Create voice intent
        Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZER_SPEECH);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
        voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity
        voiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Wrap the pending intent & bundle inside the voice intent
        voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);

        // Start the voice search
        getContext().startActivity(voiceIntent);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜