Android小部件未更新 - 具有不正确的小部件ID

时间:2012-05-14 17:19:31

标签: android widget android-appwidget appwidgetprovider

我一直在制作一个小部件并取得一些进展(我希望),但仍然无法让它做我想做的事。

我有一个配置器活动,可以正常工作。当它关闭时,我调用WordWidget类中的updateAppWidget方法,小部件更新随机数。

我无法做到这一点因为onClick而得到随机数变化。根据日志我输入onReceive()方法,但那就是它。

在日志中我打印小部件ID。我注意到,当从配置器活动运行时,它打印appWidgetId = 33,但是当我按下小部件时,它打印appWidgetId = 24。

这是代码:

public class WordWidget extends AppWidgetProvider
{

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        //This is run when a new widget is added or when the update period expires.
        Log.v("wotd",  "In onUpdate(), updating " + appWidgetIds.length + " widgets");
        Log.v("wotd",  "   the array is " + appWidgetIds.toString());

        for(int x = 0; x < appWidgetIds.length; x++)
        {
            Integer appWidgetId = appWidgetIds[x];


            //Method that updates the random value
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);
        Log.v("wotd", "In onReceive() with intent=" + intent.toString());
        if (intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE"))
        {
            Integer mAppWidgetId;
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            Bundle extras = intent.getExtras();
            if(extras != null)
            {
                Log.v("wotd", "In onReceive(), extras is valid");
                mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

                //If they gave us an intent without a valid widget Id, just bail.
                if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
                {
                    Log.v("wotd", "In onReceive(), mAppWidgetId is ok");
                    updateAppWidget(context, appWidgetManager, mAppWidgetId);
                }
            } else
            {
                Log.v("wotd", "In onReceive(), extras is INVALID");
                mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
            }
        }


    }


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, Integer appWidgetId)
    {
        Log.v("wotd", "In updateAppWidget() with appWidgetId=" + appWidgetId);

        //Setup onClick
        Intent widgetIntent = new Intent(context, WordWidget.class);
        widgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
        widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);

        Integer nextNumb = new Random().nextInt(100);
        Log.v("wotd", "In updateAppWidget(), nextNumb = " + nextNumb.toString());

        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
        remoteView.setTextViewText(R.id.mainText, String.valueOf(nextNumb));
        remoteView.setOnClickPendingIntent(R.id.mainText, widgetPendingIntent);

        // Tell the widget manager
        appWidgetManager.updateAppWidget(appWidgetId, remoteView);
    }
}

这是我触摸窗口小部件时的日志文件(请注意,prefs活动还有一个日志打印输出,并将窗口小部件ID显示为与此不同的数字):

05-14 17:15:47.453: V/wotd(1585): In onReceive() with intent=Intent { act=android.appwidget.action.APPWIDGET_UPDATE flg=0x10000000 cmp=com.rfxlabs.wordofthedaywidget/.WordWidget bnds=[202,177][222,201] (has extras) }
05-14 17:15:47.453: V/wotd(1585): In onReceive(), extras is valid
05-14 17:15:47.463: V/wotd(1585): In onReceive(), mAppWidgetId is ok
05-14 17:15:47.463: V/wotd(1585): In updateAppWidget() with appWidgetId=24

05-14 17:15:47.463:V / wotd(1585):在updateAppWidget()中,nextNumb = 42

正如您所看到的,我实际上是在输入我的updateAppWidget方法,但由于某种原因,appWidgetId不正确。

为什么会发生这种情况?非常感谢!

3 个答案:

答案 0 :(得分:21)

在您的函数updateAppWidget中,转到以下行:

PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);

...并将其更改为:

PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, widgetIntent, 0);

答案 1 :(得分:0)

在触摸时更新主屏幕小部件您需要在Manifest中注册自定义广播接收器并将其添加为小部件的操作:

第1步:将自定义接收器注册为:

<intent-filter>
<action android:name="com.imrankhanandroid.HomeWidgetRandomNum.ACTION_WIDGET_RANDOM"/>
</intent-filter>

第2步:在您的窗口小部件类AddAction中布局为:

public void onReceive(Context paramContext, Intent paramIntent)
    {
        //Intent
         String str = paramIntent.getAction();
         if (paramIntent.getAction().equals(ACTION_WIDGET_RANDOM)) {
             updateWidgetState(paramContext, str);   
         }
         else
            {
              super.onReceive(paramContext, paramIntent);
            }
    }

第3步:在您的updateWidgetState中,向Intent添加新操作:

rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain);
Intent active = new Intent(paramContext, Randomnuberwidget.class);
active.setAction(ACTION_WIDGET_RANDOM);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0);
rview.setOnClickPendingIntent(R.id.ImageView_gps, actionPendingIntent);

最后,您可以从HERE HomeWidgetRendomNumber下载每个点击生成随机数的工作HomeWidget示例。

答案 2 :(得分:0)

您需要在getBroadcast中设置最后一个标志:PendingIntent.FLAG_UPDATE_CURRENT