使用小部件上的按钮手动更新Android应用程序小部件

时间:2014-10-25 21:02:50

标签: android android-appwidget appwidgetprovider

我有一个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 个答案:

答案 0 :(得分:5)

这非常容易。下面是修改后的代码,用于制作" 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方法。无论你指定哪个。

答案 1 :(得分:5)

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);
       }
}

答案 2 :(得分:4)

如果MessMenuWidgetProvider是您的AppWidgetProvider子类,则您的PendingIntent会触发onReceive(),而不是onUpdate()。您需要在setAction()supplying ACTION_APPWIDGET_UPDATE上致电Intent,并在一个参数{{1}中填写the EXTRA_APPWIDGET_IDS extra以及您的应用小部件ID }。换句话说,您需要将int[]设置为Intent Intent结构,AppWidgetManager用于触发onUpdate()中的AppWidgetProvider

答案 3 :(得分: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);