0

アプリにウィジェットを実装しようとしていますが、PendingIntent のバンドルに問題があります。

私のウィジェットには 2 つのボタンがあり、いずれかが押されると、ウィジェットのコンテンツが更新されます。onReceiveしかし、バンドルが常にあるため、メソッドでウィジェットを更新できない場合null。何か案が?

public class StudyWidgetProvider extends AppWidgetProvider {

private static String LOG_TAG = StudyWidgetProvider.class.toString();
private static String BUTTON_BACK = "BUTTON_BACK";
private static String BUTTON_FORWARD = "BUTTON_FORWARD";

private Calendar currentDate = Calendar.getInstance();
private RemoteViews widget;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    for(int i=0; i<appWidgetIds.length; i++){
        Log.i(LOG_TAG, "Update Widget");
        Intent intent = new Intent(context, WidgetService.class);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        widget = new RemoteViews(context.getPackageName(), R.layout.widget);

        SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
        widget.setTextViewText(R.id.widget_day, dateFormater.format(currentDate.getTime()));
        dateFormater = new SimpleDateFormat("dd.MM.yyyy");
        widget.setTextViewText(R.id.widget_date, dateFormater.format(currentDate.getTime()));

        //Button-Actions
        Intent backIntent = new Intent(context, StudyWidgetProvider.class);
        backIntent.setAction(BUTTON_BACK);
        Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
        forwardIntent.setAction(BUTTON_FORWARD);
        PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        widget.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
        widget.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);

        widget.setRemoteAdapter(appWidgetIds[i], R.id.words, intent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    String action = intent.getAction();
    if(action.equals(BUTTON_BACK)){
        Log.i(LOG_TAG, "Pressed back");
        currentDate.set(Calendar.DATE, -1);
    }
    if(action.equals(BUTTON_FORWARD)){
        Log.i(LOG_TAG, "Pressed forward");
        currentDate.set(Calendar.DATE, 1);
    }

    Bundle extras = intent.getExtras();
    if(extras != null){
        Log.i(LOG_TAG, "Bundle not null");
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

        onUpdate(context, appWidgetManager, appWidgetIds);
    }
}
}
4

1 に答える 1