1

我不会删除这个问题,因为 commons 在下面提出了一些很好的观点,但我确实重新编写了代码并在这里提出了不同的问题:如何在 Widget Service 类中检索共享首选项数据而不传递不正确的默认值或获取空指针错误?

我正在开发一个应用程序,它接受用户的输入选择并将它们传递给一个小部件。它目前正在运行一项服务来管理它并且运行良好,但我无法弄清楚如何有效地将字符串从一个传递到另一个。到目前为止,这是我的代码:

        //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) {
                //I have already tried shared preferences like this:
                //This was intended to give the shared preferences a unique identifier.
                //It does not work for what I am trying to do
                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 also attempted to put items here:
                intentUpdate.putExtra("username", user_name);

                //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
                    */
     }
}
4

1 回答 1

4

如何从小部件配置类中检索 Widget Provider 类中的额外内容,以及在收到它们后如何将它们传递给服务?

你从不做任何事情开始。

AppWidgetProvider只是更新应用小部件内容的一种方式,Android 在添加应用小部件时以及根据应用小部件元数据请求定期更新时将专门使用这种方式。此外,请记住,您的实例AppWidgetProvider只使用一次,然后被丢弃。

如果你想在其他地方更新你的应用小部件,去更新应用小部件,通过创建RemoteViews并将它们提供给AppWidgetManager. 你AppWidgetProvider与它无关。引用文档

当 App Widget 使用配置 Activity 时,Activity 负责在配置完成时更新 App Widget。您可以通过直接从 AppWidgetManager 请求更新来做到这一点。

如果你想要一个通用的 update-the-app-widget 逻辑实现,把它放在你的 configuration Activity, yourAppWidgetProvider和其他任何需要更新 app widget 内容的通用类中。

所以,当用户通过activity配置app widget时,你需要:

  • 通过AppWidgetManager, 和

  • 保留配置数据(在数据库、SharedPreferences或其他类型的文件中),以便可以将其用于将来的更新

于 2015-01-10T20:29:16.723 回答