5

我有一个 Android App 小部件和小部件上的按钮。我已将更新时间段设置为 30 分钟,但我也想在触摸按钮时更新小部件。这是我的代码:

        RemoteViews remoteV = new RemoteViews(context.getPackageName(), R.layout.widgetmenu);

        Intent intentSync = new Intent(context, MessMenuWidgetProvider.class);
        PendingIntent pendingSync = PendingIntent.getBroadcast(context,0, intentSync,0);
        remoteV.setOnClickPendingIntent(R.id.imageButtonSync,pendingSync);

        appWidgetManager.updateAppWidget(awID, remoteV);

我已将更新时间设置为 30 分钟,因此每 30 分钟调用一次函数 onUpdate()。我想要实现的是使用按钮手动调用 onUpdate() 。但它没有发生。有什么帮助吗?

4

4 回答 4

7

这非常容易。下面是修改后的代码,用于在每次单击按钮时调用小部件的“onUpdate”方法。

RemoteViews remoteV = new RemoteViews(context.getPackageName(), R.layout.widgetmenu);

Intent intentSync = new Intent(context, MessMenuWidgetProvider.class);
intentSync.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); //You need to specify the action for the intent. Right now that intent is doing nothing for there is no action to be broadcasted.
PendingIntent pendingSync = PendingIntent.getBroadcast(context,0, intentSync, PendingIntent.FLAG_UPDATE_CURRENT); //You need to specify a proper flag for the intent. Or else the intent will become deleted.
remoteV.setOnClickPendingIntent(R.id.imageButtonSync,pendingSync);

appWidgetManager.updateAppWidget(awID, remoteV);

现在,每次单击该按钮时,广播AppWidgetManager.ACTION_APPWIDGET_UPDATE都会发送到您的小部件,并且您在该类中的方法将处理更新。所以要么onUpdate调用方法,要么调用onReceive方法。无论您指定了哪个。

于 2014-10-26T00:11:18.720 回答
6
RemoteViews remoteV = new RemoteViews(context.getPackageName(), R.layout.widgetmenu);

Intent intentSync = new Intent(context, MessMenuWidgetProvider.class);
intentSync.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
PendingIntent pendingSync = PendingIntent.getBroadcast(context,0, intentSync, PendingIntent.FLAG_UPDATE_CURRENT); 
remoteV.setOnClickPendingIntent(R.id.imageButtonSync,pendingSync);

appWidgetManager.updateAppWidget(awID, remoteV);

它将调用 onReceive(); 在您的 onReceive() 方法中,只需添加以下内容即可手动调用 onUpdate。

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);

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

        onUpdate(context, appWidgetManager, appWidgetIds);
       }
}
于 2014-10-26T07:13:17.860 回答
5

如果MessMenuWidgetProvider是您的AppWidgetProvider子类,您PendingIntent将触发onReceive()它,而不是onUpdate(). 您需要调用setAction(), supplyingIntent,并单参数中使用您的应用小部件的 ID填写额外内容。换句话说,您需要将 设置为与在您的.ACTION_APPWIDGET_UPDATEEXTRA_APPWIDGET_IDSint[]IntentIntentAppWidgetManageronUpdate()AppWidgetProvider

于 2014-10-25T21:21:51.077 回答
0

在 App Widget Provider 的 onUpdate() 代码中,将以下内容添加到 Intent 中以提供小部件 id:

intentSync.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);

在 App Widget Configure 活动中,确保您获得了小部件 ID:

int awID = AppWidgetManager.INVALID_APPWIDGET_ID;

 // Find the widget id from the intent. 
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            awID = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,  AppWidgetManager.INVALID_APPWIDGET_ID);
        }

    // If we got intent without the widget id, exit.
    if (awID == AppWidgetManager.INVALID_APPWIDGET_ID) {
        finish();
    }

对于按钮侦听器调用更新,然后使用以下代码传回原始小部件 ID:

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, resultValue); 
于 2014-10-25T23:58:26.960 回答