2

I am currently creating my first widget with three ImageButtons. I've followed the answer described by Clickable widgets in android pretty closely, however I fail to get it working.

I've defined three strings which are my actions:

private String playAction = "playService";
private String stopAction = "stopService";
private String resetAction = "resetService";

Next in my OnUpdate() function I add the setOnclickPendingIntent:

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

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    remoteViews.setOnClickPendingIntent(R.id.widget_play, getPendingSelfIntent(context, playAction));
    remoteViews.setOnClickPendingIntent(R.id.widget_pause, getPendingSelfIntent(context, stopAction));
    remoteViews.setOnClickPendingIntent(R.id.widget_reset, getPendingSelfIntent(context, resetAction));

    manager = appWidgetManager;

    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);

    // Update the widgets via the service
    context.startService(intent);
}

The getPendingSelfIntent function is defined as followed:

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, MyWidgetProvider.class);
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

However the onReceive function receives none of the above defined actions. I currently log all the actions of incoming intents, but the only one that passes is the android.appwidget.action.APPWIDGET_UPDATE action.

For the sake of completion, this is how I've defined the actions in my Manifest:

    <receiver
        android:name="MyWidgetProvider"
        android:icon="@drawable/ic_launcher"
        android:label="My Widget">

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="playService" />
            <action android:name="stopService" />
            <action android:name="resetService" />
        </intent-filter>
    </receiver>

I've read in the Android docs that elements that are part of a collection (ListView etc.) cannot be set using the setOnClickPendingIntent function. I have my three buttons in my layout. To make sure this is not causing the error, here's what the XML looks like:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/widget_play"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:layout_marginRight="5dp"
            android:src="@drawable/run"/>

        <ImageButton
            android:id="@+id/widget_pause"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:layout_marginRight="5dp"
            android:src="@drawable/pause"/>

        <ImageButton
            android:id="@+id/widget_reset"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/reset"/>

    </LinearLayout>

Any help on this matter is appreciated, as I am all out of ideas. I've searched SO for similar problems yet they all describe what I've already tried.

Community
  • 1
  • 1
Gooey
  • 4,740
  • 10
  • 42
  • 76
  • Post your UpdateWidgetService class – San Aug 12 '14 at 19:25
  • 1
    I think you should be using `PendingIntent.FLAG_UPDATE_CURRENT` as the last argument to `PendingIntent.getBroadcast(...)`. Try replacing `return PendingIntent.getBroadcast(context, 0, intent, 0);` with `return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);`. – Vikram Aug 12 '14 at 20:41

1 Answers1

4

I think you are missing the update to the widget after you set the pendingintents:

appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

If this doesn't help, you should post your UpdateWidgetService.class too.

Btw what does the UpdateWidgetService.class do?

Tamás
  • 302
  • 1
  • 7
  • 2
    This answer is correct, `updateAppWidget` need to be called. I've created a [gist](https://gist.github.com/manishcm/bd05dff09b5b1640d25f) which has working files. Also there is no need to add custom actions in manifest file, as specified in the [documentation](http://developer.android.com/guide/topics/appwidgets/index.html#Manifest). If this answer is acceptable by OP, this comment content can be appended to the answer. – Manish Mulimani Aug 13 '14 at 07:36
  • Can you explain what this function internally does? The documentation does not provide the whole story to me. I'll try your suggestion now. – Gooey Aug 13 '14 at 10:55
  • This function does the update on the widget itself. You prepare the remoteviews, and with this function you pass it to the widget with the given id. You can update all your widgets with the same content, or each widget with a different content, that depends if you pass all the id-s in an array or one id. You can view the documentation here: [link](https://developer.android.com/reference/android/appwidget/AppWidgetManager.html), and the source code for it here: [link](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/appwidget/AppWidgetManager.java) – Tamás Aug 13 '14 at 11:06
  • I can give the bounty in 5 hours, but you can count on it. – Gooey Aug 13 '14 at 11:21