0

我有一个带有配置活动的应用程序小部件。当我在配置活动中单击“完成”时,小部件就会出现。如果这是我第一次使用小部件,它将是空白的,因为这不是第一次,它会使用我之前使用配置活动的数据进行更新。小部件从 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();
}
4

1 回答 1

1

SharedPreferences 是滞后的。您可以在 AppWidgetProvider 的 onUpdate 中获取该代码并在 ConfigurationActivity 中调用它(这将确保至少在第一次部署后立即显示该值)。

于 2012-11-23T09:14:25.807 回答