Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6860169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:22:29+00:00 2026-05-27T02:22:29+00:00

I have seen many questions along these lines, and I keep seeing the same

  • 0

I have seen many questions along these lines, and I keep seeing the same answer over and over. I don’t want to have to have a Service for every kind of widget my app has, especially seeing as my app already has 2 persistent services.

Specifically, if one of my existing services sees that data has changed, I want to update my widgets. Doing this on a timer is annoying, as it could be days between updates or there might be several within one hour. I want my widgets to ALWAYS show up to date information.

Android widget design seems to work on the basis that your widget pulls information when it wants it, I think there are many sensible scenarios where an activity may wish to push data to a widget.

Read the answer below for how I worked out how to do precisely this. As far as I can see there are no adverse effects if it is done properly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:22:30+00:00Added an answer on May 27, 2026 at 2:22 am

    To force our widgets for a particular widget provider to be updated, we will need to do the following:

    • Set up our provider to receive a specialized broadcast
    • Send the specialized broadcast with:
      1. All the current widget Id’s associated with the provider
      2. The data to send
      3. Keys that only our provider responds to (Important!)
    • Getting our widget to update on click (without a service!)

    Step 1 – Set up our AppWidgetProvider

    I will not go through the details of creating the info xml file or changes to the Android Manifest – if you don’t know how to create a widget properly, then there are plenty of tutorials out there you should read first.

    Here is an example AppWidgetProvider Class:

    public class MyWidgetProvider extends AppWidgetProvider {
    
    public static final String WIDGET_IDS_KEY ="mywidgetproviderwidgetids";
    public static final String WIDGET_DATA_KEY ="mywidgetproviderwidgetdata";
    
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.hasExtra(WIDGET_IDS_KEY)) {
            int[] ids = intent.getExtras().getIntArray(WIDGET_IDS_KEY);
            if (intent.hasExtra(WIDGET_DATA_KEY)) {
               Object data = intent.getExtras().getParcelable(WIDGET_DATA_KEY);
               this.update(context, AppWidgetManager.getInstance(context), ids, data);
            } else {
                this.onUpdate(context, AppWidgetManager.getInstance(context), ids);
            }
        } else super.onReceive(context, intent);
    }
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        update(context, appWidgetManager, appWidgetIds, null);
    }
    
    //This is where we do the actual updating
    public void update(Context context, AppWidgetmanager manager, int[] ids, Object data) {
    
        //data will contain some predetermined data, but it may be null
       for (int widgetId : ids) {
            .
            .
            //Update Widget here
            .
            .
            manager.updateAppWidget(widgetId, remoteViews);
        }
    }
    

    Step 2 – Sending the broadcast

    Here we can create a static method that will get our widgets to update. It is important that we use our own keys with the widget update action, if we use AppWidgetmanager.EXTRA_WIDGET_IDS we will not only break our own widget, but others as well.

    public static void updateMyWidgets(Context context, Parcelable data) {
        AppWidgetManager man = AppWidgetManager.getInstance(context);
        int[] ids = man.getAppWidgetIds(
                new ComponentName(context,MyWidgetProvider.class));
        Intent updateIntent = new Intent();
        updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        updateIntent.putExtra(MyWidgetProvider.WIDGET_ID_KEY, ids);
        updateIntent.putExtra(MyWidgetProvider.WIDGET_DATA_KEY, data);
        context.sendBroadcast(updateIntent);
    }
    

    If using this method with multiple providers MAKE SURE they use different keys. Otherwise you may find widget a’s code updating widget b and that can have some bizarre consequences.

    Step 3 – updating on click

    Another nice thing to do is to get our widget to update magiacally whenever it is clicked. Add the following code into the update method to acheive this:

    RemoteViews views = 
          new RemoteViews(context.getPackageName(),R.layout.mywidget_layout);
    
    Intent updateIntent = new Intent();
    updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    updateIntent.putExtra(myWidgetProvider.WIDGET_IDS_KEY, ids);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
          context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    views.setOnClickPendingIntent(R.id.view_container, pendingIntent);
    

    This code will cause the onUpdate method to be called whenever the widget is clicked on.

    Notes

    • Any call to updateWidgets() will cause all instances of our widget to be updated.
    • Clicking the widget will cause all instances of our widget to be updated
    • NO SERVICE REQUIRED
    • Of course, be careful not to update to often – widget updates use battery power.
    • Bear in mind that the broadcast is received by ALL widget providers, and its our special keys that ensure it is only our widgets that actually update.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen many related questions but didn't find any answer. I have successfully
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
Haven't seen many Geneva related questions yet, I have posted this question in the
I have seen many answers on stackoverflow, but I didn't find an answer that
I have seen many posts and questions about Mocking a private method but still
I have seen many questions about the above topics but none that address this.
I have seen many questions on SO about this particular subject but none of
I've seen many questions about this, but i've never really got the answer that
I have seen many questions asking how to validate the presence of an association,
I have seen in many questions on SO, asking for your opinion of how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.