我不会删除此问题,因为公用之处在下面提出了一些要点,但是我确实对代码进行了修改,并在此处以不同的方式提出了问题:如何在Widget Service类中检索共享的首选项数据,而不传递错误的默认值或获取空指针错误?
我正在开发一个需要用户输入选择并将其传递给小部件的应用程序。它当前正在运行一项服务来对其进行管理,并且运行良好,但是我无法弄清楚如何有效地将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
*/
}
}
如何从小部件配置类中检索小部件提供程序类中的额外内容,以及如何在收到它们后将其传递给服务?
您首先不做任何事情。
您AppWidgetProvider
仅仅是更新应用程序小部件内容的一种方法,一种将在添加应用程序小部件时以及在应用程序小部件元数据请求进行定期更新时由Android专门使用的一种方法。此外,请记住,您的实例AppWidgetProvider
仅使用一次,然后被丢弃。
如果您想在其他地方更新您的应用程序小部件,请通过创建RemoteViews
并将其赋予来更新应用程序小部件AppWidgetManager
。您AppWidgetProvider
与它无关。引用文档:
当应用程序小部件使用配置活动时,活动负责在配置完成后更新应用程序小部件。您可以通过直接从AppWidgetManager请求更新来实现。
如果您希望有一个update-the-app-widget逻辑的通用实现,请把该通用类用于您的configuration Activity
,yourAppWidgetProvider
以及需要更新应用程序小部件内容的任何其他类。
因此,当用户通过活动配置应用程序小部件时,您需要:
通过AppWidgetManager
和自己更新应用程序小部件
保留配置数据(在数据库,SharedPreferences
或其他类型的文件中),以便可以将其用于将来的更新
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句