How to know clicked widget id?

2019-02-19 13:13发布

问题:

I have implemented a widget with an ImageButton and a TextView. That ImageButton launch an activity when its clicked. This activity updates the widget text with what the user writes on the activity EditText. Now the problem is that I only know how to get the ids like this:

for (int widgetId : allWidgetIds) {
        // Create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(getApplicationContext()
                .getPackageName(), R.layout.widget_layout);
        Log.w("WidgetExample", String.valueOf(number));

        //Here I should set text from edit text, but I'm using a random for testing.

        remoteViews.setTextViewText(R.id.textView1,
                "Random: " + String.valueOf(number));

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }

This code will obviously change the data of all the ids, since its inside a for. Is there anyway that I can past the clicked widgetId with my intent, so I can eliminate that for? This is my widgetProvider:

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, Widget.class);
    allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {

        Intent i = new Intent(context, WidgetSetup.class);
        i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
        i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

        // Create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        // Set the text
        remoteViews.setTextViewText(R.id.textView1, String.valueOf(number));

        Intent active = new Intent(context, Widget.class);
        active.setAction(ACTION_WIDGET_REFRESH);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.imageButton1, actionPendingIntent);

        active = new Intent(context, Widget.class);
        active.setAction(ACTION_WIDGET_SETTINGS);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.imageButton2, actionPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
        //Log.i("onReceive", ACTION_WIDGET_REFRESH);

        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

    } else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) {
        //Log.i("onReceive", AppWidgetManager.EXTRA_APPWIDGET_ID);



        Intent i = new Intent(context, WidgetSetup.class);
        i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        //Here I tried to pass the widgetID with no luck.
        //i.putExtra(pass widget id?);

        i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

    } else {
        super.onReceive(context, intent);
    }
}

Thanks

回答1:

For a running example have a look at my code for MiniCallWidget lines 97 and 169.



回答2:

Add an Extra to the intent you launch onClick and create in the for loop that specifies the ID of the current widget being setup.

Then retrieve the ID from the extras in the receiving Activity, and pass it back when you're done. Then use the returned ID to make changes only to one widget.

You can do this by having an if-else that also checks for a flag that tells whether or not you're updating only one widget.