سؤال

I want to get the appwidget layout associated with an appwidget id.

In the Widget provider class for an appwidget, the android docs provide an easy way to get the appwidget ids...

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];


But they IDs dont allow me to know which appwidget provider it comes from (multiple widget providers and layouts.).

EDIT: i am using a service class to update the widgets, not the widget provider class.

in the service class, i want to get the appwidget layout for each id so that i can use remoteviews to update that layout. I have tried a few different suggestions, but none of them give me the value that i need to use with remoteviews. eg "R.id.widget_layout"

How can i get a value like "R.id.widget_layout" from an appwidget ID in the correct "int" form to use with remoteviews?

هل كانت مفيدة؟

المحلول 2

Because i have more than one widget provider class:

  • I have a separate set of code to update the widgets for each provider. I couldnt find a way to have one set of code update all widgets for all widget providers (but i continue to believe it is possible).

Im using a service class to update the widgets, so i am using "ComponentName" to get the widgetIds for each widget provider class.

My solution is this, repeated for each widget provider class:

// update all 2x2 widget instances
ComponentName thisWidget2x2 = new ComponentName(getBaseContext(), WidgetProvider2x2.class);
int[] allWidgetIds2x2 = localAppWidgetManager.getAppWidgetIds(thisWidget2x2);
for (int widgetId : allWidgetIds2x2) {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout_2x2);
    remoteViews.setImageViewResource(R.id.imageviewA_B, timePhraseA_B);
    remoteViews.setOnClickPendingIntent(R.id.topLayout, openAwcPi);
    localAppWidgetManager.updateAppWidget(widgetId, remoteViews);
}

This is repeated for each widget provider

نصائح أخرى

You can get the AppWidget layout ID and info about the provider for each AppWidget this way:

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

    for(int i=0; i<N; i++) {

        int appWidgetId = appWidgetIds[i];

        // you can use layoutId variable instead of R.id.widget_layout
        int layoutId = appWidgetManager.getAppWidgetInfo(appWidgetId).initialLayout;
        // String yourProvider will be something like .WidgetProvider2x2
        String yourProvider = appWidgetManager.getAppWidgetInfo(appWidgetId).provider.getShortClassName();

    }

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top