2

I am trying to run a widget in my app. The widget data (sync frequency, username, etc) is decided in the WidgetConfig class. It then opens a widget provider, which creates the widget, and lastly a service updates the views and makes the server calls to update data.

Here is some of my code:

    //First Widget config is called:
    public class WidgetConfig extends Activity{

        //Stuff happens here to get data from EditTexts and spinners and converts 
        //them to strings.
        //Eventually a button is pressed which enters all the information:

        public void onClick(View v) {
            //Creates a unique string with the app widget ID
            String str = Integer.toString(appWidgetId); 

            sp.putString(editor, str + "::" + "username", user_name);
            //The appWidgetID was unique and so I thought it would work as an
            //identifier for shared prefs.

            //This is the intent for opening the provider
            Intent intentUpdate = new Intent(context, MailWidgetProvider.class); 

            //I left out the rest of the pending update code as it is irrelevant to this.
        }
    }


    //Next the AppWidgetProvider is called
    public class MailWidgetProvider extends AppWidgetProvider {

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

            ComponentName thisWidget = new ComponentName(context, 
                                   MailWidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

            //This is the intent to open up and run the service
            Intent intent = new Intent(context.getApplicationContext(),                            
                                        MailWidgetUpdateService.class);

            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

            context.startService(intent);
        }

    }

//Service Class
public class MailWidgetUpdateService extends Service {
    public void onStart(Intent intent, int startId) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
                    .getApplicationContext());

            int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

            ComponentName thisWidget = new ComponentName(getApplicationContext(),
                    MailWidgetProvider.class);

            int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);

            //Loop through the IDs
            for (int widgetId : allWidgetIds) {

                int awid = widgetId;
                String str = Integer.toString(widgetId);

                String user_name = sp.getString(settings, str + "::" + "chosen_accout_string", "Loading...");
                Log.d(TRACKING_USERNAME, user_name);

                /*
                Issue Here, see explanation below
                */
 }

}

Although I am updating my shared preferences with a unique identifier and the respective data

String str = Integer.toString(appWidgetId); 
sp.putString(editor, str + "::" + "username", user_name);) 

It would look like this: ("2749::username)

Whenever I run the code in the service class to pull the data, it loops through what SHOULD only be 1 app widget ID (seems like 2 in the array for some reason) and pulls the data.

The first loop pulls nothing and either gives an incorrect default value (Like loading... above) or a null pointer if I do not have a default value.

How do I retrieve the data in my Service class from the shared preferences when it seems to be passing more than one widget ID for only 1 widget?

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • Well, your `AppWidgetProvider` is going to be called with all your app widget IDs. And sometimes Android gets confused and thinks that you have an app widget around when you don't. To clear that up, try uninstalling and reinstalling. But if there is something you need to do uniquely for the one app widget that was just configured, either the activity has to do that, or the activity has to tell the service directly to do that. – CommonsWare Jan 11 '15 at 01:12
  • What you are saying makes sense, but the issue I am having is that when I create the widget in the widget config class, I use the app widget ID to create a shared preference. When it passes to the service, there are now magically 2 app widget IDs, despite just having one widget, it that app widget ID that is causing problems. Is it normal to get 2 app widget IDs passed in instead of 1 for a single widget? – PGMacDesign Jan 11 '15 at 06:05
  • "When it passes to the service" -- the activity does not talk to the service, according to your code. "Is it normal to get 2 app widget IDs passed in instead of 1 for a single widget?" -- this is covered in my previous comment. – CommonsWare Jan 11 '15 at 12:02

0 Answers0