0

我正在尝试为我的应用程序实现一个小部件,但我遇到了 PendingIntent 捆绑包的问题。

我的小部件有两个按钮,如果一个被按下,小部件应该更新其内容。但是,如果我无法更新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

0

您没有在 Intent 中添加任何额外内容,那么您为什么期望会有任何内容?

于 2013-11-21T08:08:59.817 回答