Clickable widgets in android

First, add a static variable with a constant.

public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";

Then you need to add the action to the intent before you add the intent to the pending intent:

Intent intent = new Intent(context, widget.class);
intent.setAction(YOUR_AWESOME_ACTION);

(Where widget.class is the class of your AppWidgetProvider, your current class)

You then need to create a PendingIntent with getBroadcast

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Set the onClickPendingIntent for the clickable view in your widget

remoteView.setOnClickPendingIntent(R.id.widgetFrameLayout, pendingIntent);

Next, override the onReceive method in the same class:

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

And then respond to your button presses by querying the intent returned for your action within the onReceive method:

if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {
   //do some really cool stuff here
}

And that should do it!

Leave a Comment