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

时间:2015-01-10 20:20:52

标签: android android-intent widget appwidgetprovider extras

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

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

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

1 个答案:

答案 0 :(得分:4)

  

如何从窗口小部件配置类中检索窗口小部件提供程序类中的附加内容,如何在收到后将它们传递给服务?

你从没有做太多的事情开始。

您的AppWidgetProvider仅仅是一个更新应用小部件内容的方式,当您的应用小部件添加时,Android将专门用于应用小部件内容,以及应用请求定期更新窗口小部件此外,请记住,AppWidgetProvider的实例仅使用一次,然后被丢弃。

如果您想在其他地方更新您的应用小部件,请更新应用小部件,方法是创建RemoteViews并将其提供给AppWidgetManager。您的AppWidgetProvider与此无关。引用the documentation

  

当App Widget使用配置Activity时,Activity负责在配置完成时更新App Widget。您可以通过直接从AppWidgetManager请求更新来完成此操作。

如果您想拥有update-the-app-widget逻辑的通用实现,请将其配置为ActivityAppWidgetProvider以及其他任何内容使用的常用类。需要更新app小部件内容。

因此,当用户通过活动配置应用小部件时,您需要:

  • 通过AppWidgetManager

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

相关问题