Widget ImageButton listener is not always called

I have a simple widget (Android 2.1) containing only LinearLayout, itself containing ImageButton. ImageButton has a listener. The problem is that if I put several of the same widgets on my home screen, some of them work (the listener is called at the click of a button), and some do not! I do not see a single template in which work, and which are not.

Here is the widget layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ImageButton01"
    android:src="@drawable/widget_running"
    android:background="@null">
</ImageButton>

And here is the widget provider code:

public class GPAppWidgetProvider extends AppWidgetProvider {
private String mTag = getClass().getSimpleName();

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    Log.e(mTag, "onUpdate ");

    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
        Log.e(mTag, "widget onUpdate one loop");
        int appWidgetId = appWidgetIds[i];

        // Create an Intent
        Intent intent = new Intent(context, GPService.class).setAction(ACTION_WIDGET_TOGGLE_PAUSE);
        intent.putExtra("widgetId", appWidgetId);
        PendingIntent pauseIntent = PendingIntent.getService(context, 0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.gp_appwidget);
        views.setOnClickPendingIntent(R.id.ImageButton01, pauseIntent);

        // widget update
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

}

+5
source share
1 answer

I had the same problem.

Remember to set another "requestCode" when you call "getService":

public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)

And make sure your "appWidgetId" is different for each widget.

0

All Articles