如何通过意图将数据从小部件配置 class 传递到小部件提供程序 class?

How do I pass data from a widget config class to a widget provider class via intents?

我不打算删除这个问题,因为 commons 在下面提出了一些很好的观点,但我确实修改了代码并在这里提出了不同的问题:How do I retrieve shared preferences data in a Widget Service class without passing in incorrect default values or getting null pointer errors?

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

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

How do I retrieve the extras in the Widget Provider class from the widget config class and how do I go about passing them on to the service after receiving them?

你首先不要做太多这些事情。

您的 AppWidgetProvider 只是 一种 更新应用程序小部件内容的方法,当添加您的应用程序小部件时,Android 将专门使用这种方法并根据您的应用程序小部件元数据的要求进行定期更新。此外,请记住,您的 AppWidgetProvider 实例仅使用一次,然后被丢弃。

如果您想在其他地方更新您的应用程序小部件,去更新应用程序小部件,方法是创建 RemoteViews 并将它们提供给 AppWidgetManager.你的 AppWidgetProvider 与它无关。引用 the documentation:

When an App Widget uses a configuration Activity, it is the responsibility of the Activity to update the App Widget when configuration is complete. You can do so by requesting an update directly from the AppWidgetManager.

如果您想要更新应用程序小部件逻辑的通用实现,请将其放在您的配置 Activity、您的 [=10] 使用的一些通用 class =],以及任何其他需要更新应用小部件内容的内容。

因此,当用户通过 activity 配置应用程序小部件时,您需要:

  • 通过 AppWidgetManager

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