I have an app widget with a configuration activity. When I click "done" in the config activity the widget come out. If that's the first time I use the widget, it will be blank, in that's not the first time, it update with the data from my previous use of the configuration activity. The widget gets its data from SharedPrefences. Previous instance of the widget won't get updated with the new data. No error on LogCat. Here is some code: 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);
}

Configure Activity (some part):

   //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();
}
有帮助吗?

解决方案

SharedPreferences are laggy. You could take that code in onUpdate of your AppWidgetProvider and call it in your ConfigurationActivity (which'll ensure the value is immediately displayed after the first deployment at least).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top