我有一个配置活动的app小部件。当我在配置活动中单击“完成”时,小部件就会出现。如果这是我第一次使用小部件,它将是空白的,这不是第一次,它使用我之前使用配置活动的数据进行更新。小部件从SharedPrefences获取其数据。以前的小部件实例不会使用新数据进行更新。 LogCat没有错误。这是一些代码: 的AppWidgetProvider
public class DogTagWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds)
{
updateWidget(context, appWidgetManager, widgetId);
}
}
public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId){
//Get RemoteViews and SharedPreferences
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.dogtag_widget);
SharedPreferences settings = context.getSharedPreferences(DogTagConfigure.PREFS_NAME, Context.MODE_MULTI_PROCESS);
//Check Name
String name = settings.getString("name", null);
boolean nameB = settings.getBoolean("nameB", false);
if(!nameB){
views.setViewVisibility(R.id.llName, View.GONE);
}else {
views.setTextViewText(R.id.tvName, name);
}
//Check Address
String address = settings.getString("address", null);
boolean addressB = settings.getBoolean("addressB", false);
if(!addressB){
views.setViewVisibility(R.id.llAddress, View.GONE);
}else {
views.setTextViewText(R.id.tvAddress, address);
}
//Check Phone Number
String phoneNumber = settings.getString("phoneNumber", null);
Boolean phoneNumberB = settings.getBoolean("phoneNumberB", false);
if(!phoneNumberB){
views.setViewVisibility(R.id.llNumber, View.GONE);
}else {
views.setTextViewText(R.id.tvNumber, phoneNumber);
}
//TODO Show Owner Photo
String photoUri = settings.getString("photoUri", null);
if(!(photoUri==null)){
Uri photoURI = Uri.parse(photoUri);
views.setImageViewUri(R.id.ivPhoto, photoURI);
}else{
views.setViewVisibility(R.id.ivPhoto, View.GONE);
}
//TODO Cancel the remove and write code for this action
views.setViewVisibility(R.id.llSend, View.GONE);
/*
Intent labelIntent = get_ACTION_APPWIDGET_UPDATE_Intent(context);
PendingIntent labelPendingIntent = PendingIntent.getBroadcast(context,
appWidgetId,
labelIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
*/
Log.d("updateAppWidget", "Updated ID: " + appWidgetId);
//Update Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
配置活动(某些部分):
//DONE pressed
public void done(){
savePref();
Intent firstUpdate = new Intent(context, DogTagWidgetProvider.class);
firstUpdate.setAction("android.appwidget.action.APPWIDGET_UPDATE");
firstUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
context.sendBroadcast(firstUpdate);
Log.d("Ok Button", "First onUpdate broadcast sent");
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
finish();
}
//DISCARD pressed
public void discard(){
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_CANCELED, resultValue);
finish();
}
//Save new preferences
private void savePref() {
name = etName.getText().toString();
address = etAddress.getText().toString();
phoneNumber = etNumber.getText().toString();
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", name);
editor.putBoolean("nameB", nameB);
editor.putString("address", address);
editor.putBoolean("addressB", addressB);
editor.putString("phoneNumber", phoneNumber);
editor.putBoolean("phoneNumberB", phoneNumberB);
editor.putString("photoUri", photoUri);
editor.putBoolean("ownerFound", ownerFound);
editor.commit();
}
答案 0 :(得分:1)
SharedPreferences是滞后的。您可以在AppWidgetProvider的onUpdate中获取该代码,并在ConfigurationActivity中调用它(这将确保在第一次部署后立即显示该值)。