9

I have an appwidget that I'm trying to update from an activity.

To do that, I need the appwidget id.

I've used AppWidgetManager.getAppWidgetIds but it always returns an empty list.

I also used AppWidgetManager.getInstalledProviders to make sure that my ComponentName is correct, but still I get an empty list.

I've seen all the other questions about this, but I couldn't find something that worked for me.

Is there another way to solve this? or another way to update the widget?

My code:

ComponentName name = new ComponentName(packageName, boardcastReceiverClass);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int[] ids = appWidgetManager.getAppWidgetIds(name);
if (ids != null && ids.length > 0) {
   getApplicationContext().sendBroadcast(getUpdateIntent(ids[0]));
}

Thanks.

UPDATE: I should mention that my AppWidgetProvider is in a library project. According to the ComponentName I get with getInstalledProviders, I used the package name of my app and the class name with the package name of the library.

3
  • share your code where you think the mistake is...
    – Gopal Gopi
    Dec 4, 2013 at 10:51
  • getAppWidgetIds() will return empty list if user has not enabled/installed your Widget on Home screen...
    – Gopal Gopi
    Dec 4, 2013 at 11:12
  • @GopalRao yes, I know. I tested it when the widget was on the home screen.
    – Ran
    Dec 4, 2013 at 11:25

3 Answers 3

14
+50

You can access your widget without knowing id

appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(),Widget.class.getName()), views);

This let you access the widget without having to know the 'appWidgetID'. Then in activity:

Intent intent = new Intent(this, Settings.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
finish();
2
  • This seems like the answer to my question as well, but I am having trouble identifying the parameters. The parameters you mention [appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(),Widget.class.getName()), views);], First, is the widget class the widget provider? the widget config? Or a service from the widget? And second, the views, I am not passing in any views objects, to what is the views referring to? What would be an example of a view within a service class? Thanks in advance for your help! Jan 23, 2015 at 8:22
  • Scratch the views question, I figured out it means remoteViews. Jan 23, 2015 at 8:44
1

I'm working on something similar and here is the simplest way I got it to work.

In your service class put this wherever you want to trigger a widget update:

Intent brIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
sendBroadcast(brIntent);

This will cause the onReceive(Context context, Intent intent) method in your AppWidgetProvider class to be called. Then onRecieve can call code to update the widget. For a sample, here's what I used. I'm merely updating the text of the widget:

        //Sample output text
    String text = "My cat's name is wiggles.";
    //get an AppWidgetManager
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    //getComponentName
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    //get the IDs for all the instances of this widget
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    //update all of the widgets
    for (int widgetId : allWidgetIds) {
        //get any views in this instance of the widget
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        // Set the text
        remoteViews.setTextViewText(R.id.widgetField,  text);
        //update the widget with any change we just made
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
0

To get the app widget ID.. use this below code

onReceive() method use this below code

int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);

in onUpdate() you get the list of ids already as the parameter!

If you want to update it from activity use this

 Intent intent = new Intent(this, [activityname].class);
    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), [activityname].class));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
    sendBroadcast(intent);
1
  • I'm trying to update the widget from an activity, not from the receiver, so I don't have onReceieve.
    – Ran
    Dec 4, 2013 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.