How do I retrieve shared preferences data in a Widget Service class without passing in incorrect default values or getting null pointer errors?

我只是一个虾纸丫 提交于 2020-01-24 17:05:25

问题


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?

来源:https://stackoverflow.com/questions/27882598/how-do-i-retrieve-shared-preferences-data-in-a-widget-service-class-without-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!