0

I have a widget that have only one button.. I read that the Listener should be set like this :

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

views.setOnClickPendingIntent(R.id.button1, pendingIntent);

But I have this code :

            try {
            sr = SpeechRecognizer
                    .createSpeechRecognizer(context);
            sr.setRecognitionListener(new MainActivity().new listener());

            Intent intent = new Intent(
                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                    "voice.recognition.test");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
            sr.startListening(intent);

        } catch (Exception e) {
            Toast.makeText(context, "Exc: " + e, Toast.LENGTH_LONG).show();
        }

that should be the onClick event.. I can't make that a pending intent..

Any Idea ? :)

Muhammad Ashraf
  • 1,252
  • 1
  • 14
  • 32

1 Answers1

0

You can handle PendingIntent in your widget provider.

The following is example:

1) Set intent to button click:

final Intent intentButton = new Intent(context, MyWidgetProvider.class);

intentButton.setAction(ACTION_MY_BUTTON);

final PendingIntent pendingIntentButton =
        PendingIntent.getBroadcast(context, 0, intentButton, PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntentButton);

2) Handle intent in your MyWidgetProvider:

@Override
public void onReceive(final Context context, final Intent intent){
    super.onReceive(context, intent);

    final String action = intent.getAction();

    if (ACTION_MY_BUTTON.equals(action)) {
        // Do your stuff here!
    }
sandrstar
  • 12,503
  • 8
  • 58
  • 65