Android部件不更新 - 有不正确的控件的ID [英] Android widget not updating - has incorrect widget ID

查看:250
本文介绍了Android部件不更新 - 有不正确的控件的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在一个小部件,使一些进展(我希望),但仍不能得到它做我想做的。

I've been working on a widget and making some advances (I hope) but still can't get it to do what I want.

我有一个配置的活动而工作正常。当它关闭我打电话了WordWidget类的updateAppWidget方法和小部件更新随机数。

I have a configurator activity which works ok. When it closes I call the updateAppWidget method in the WordWidget class and the widget updates the random number.

我不能做什么就得到随机数,因为的onClick的改变。根据日志我输入的onReceive()方法,但多数民众赞成它。

What I can't do it get the random number to change because of onClick. According to the log I do enter the onReceive() method, but thats about it.

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

In the log I'm printing the widget ID. I noticed that when run from the configurator activity it's printing appWidgetId=33, but when I press the widget it prints appWidgetId=24.

这是在code:

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为一个不同的数字,该):

This is the log file, from when I touch the widget (note that the prefs activity also has a log printout and shows the widget ID as a different number that this):

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

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

因此​​,大家可以看到,实际上,我进入我的updateAppWidget方法,但由于某些原因,appWidgetId不正确。

So as you can see, I'm actually entering my updateAppWidget method, but for some reason the appWidgetId is incorrect.

任何想法,为什么发生这种情况?非常感谢!

Any ideas why this is happening? Thanks a lot!

推荐答案

在你的函数 updateAppWidget 进入这一行:

In your function updateAppWidget go to this line:

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

...并更改为:

...and change it into:

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

这篇关于Android部件不更新 - 有不正确的控件的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆