構成アクティビティを含むアプリ ウィジェットがあります。構成アクティビティで「完了」をクリックすると、ウィジェットが出てきます。ウィジェットを初めて使用する場合は空白になりますが、初めてではないため、構成アクティビティを以前に使用したときのデータで更新されます。ウィジェットはそのデータを 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();
}