chris.d
chris.d

Reputation: 53

Cannot get Widget ID in Activity

I am new to this and this is a big headache for me. I have a widget with a provider, a config and an extra activity which is called when you click on the widget. I need to determine the widget ID in the activity.

In the provider I set it to call the activity from onUpdate with a .setOnClickPendingIntent, and in the intent I add the widget ID. When I try to get the widget ID from the intent in the activity, it is always 0 (but the activity is called fine). I can retrieve the ID in the config but not in the activity with the same code.

I'm sure it will be something basic/an amateur mistake. Your help is appreciated!

Truncated code:

Provider:

public static String ACTION_WIDGET_CLICK = "ClickWidget";


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        ComponentName thisWidget = new ComponentName(context,
        HelloWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            Intent clickIntent = new Intent(context, test.class);
            clickIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            clickIntent.setAction(ACTION_WIDGET_CLICK);
            PendingIntent clickPendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
            views.setOnClickPendingIntent(R.id.update, clickPendingIntent);
            appWidgetManager.updateAppWidget(widgetId, views);
        }
    }

So on click, it will call the pendingIntent which will call the activity defined by ACTION_WIDGET_CLICK. Manifest:

<activity android:name=".test">
<intent-filter>
<action
android:name="com.example.widget_create.HelloWidgetProvider.ACTION_WIDGET_CLICK"/>
</intent-filter>
</activity>

So this calls the test actvity:

public class test extends Activity {

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent clickIntent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {         
            mAppWidgetId = extras.getInt(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
        }
    }

It doesn't receive the extras and then the widget ID gets set to 0

Upvotes: 0

Views: 1494

Answers (3)

user2741005
user2741005

Reputation:

If the same value is given by getExtras in (for example) the activity, you must change the requestCode (perhaps to the widgetid)

Intent clickIntent = new Intent(context,ActivityName.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,myWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, myWidgetId, clickIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.layout,pendingIntent);

Upvotes: 1

vggonz
vggonz

Reputation: 1986

At onUpdateyou are passing IDS (array of ID)

clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

while at your activity your are asking por ID (just one ID)

mAppWidgetId = extras.getInt(
    AppWidgetManager.EXTRA_APPWIDGET_ID,
    AppWidgetManager.INVALID_APPWIDGET_ID);

As you are inside a for looping throught every widget ID, you should change the intent at the provider with:

clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

Notice the EXTRA_APPWIDGET_ID and the widgetId value instead of the old appWidgetIds.

Upvotes: 0

athom
athom

Reputation: 313

Can you copy/paste what you get into the console log please ?

Check your Manifest file for the android:name tag, it must be android:name=".Test" with an uppercase.

Upvotes: 0

Related Questions