自我更新每隔10秒小部件Handler.postDelayed问题 [英] Self-updating every 10 seconds widget Handler.postDelayed problem

查看:270
本文介绍了自我更新每隔10秒小部件Handler.postDelayed问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让工作在Android窗口小部件自动更新的功能,简单的东西如改变它的两个TextViews每10秒。理想的解决办法是让类似精灵小部件(新闻与天气)。到目前为止,它的工作好:它通过Handler.postDelayed的Runnable的每10秒更新

I'm trying to make work a self-updating functionality in an Android widget, something simple as changing two TextViews of it every 10 seconds. The ideal solution would be to make it similar to the genie widget (news & weather). And so far, it works okay: it updates every 10 seconds via Handler.postDelayed's Runnable.

Runnable updateWidgetText = new Runnable()
{
    @Override
    public void run() {
        if (screenEnabled) {
            AppWidgetManager gm = AppWidgetManager.getInstance(ctx);                        
            ComponentName thisWidget = new ComponentName(ctx,AnimeUndergroundWidget.class);         

            int index = (int)(REFRESH_COUNT % WIDGET_NEWS_TO_SHOW);
            Noticia n = AUnder.single().getNoticiaByIndex(index);
            if (n!=null)
            {
                last = n;
                RemoteViews views = new RemoteViews(ctx.getPackageName(),R.layout.auwidget);    
                views.setTextViewText(R.id.widget_textview, n.getTitulo());
                views.setTextViewText(R.id.widget_textototal, n.getTexto().replace("\n", ""));  

                Intent clickintent = new Intent(INTENT_GO_TO_NEW);
                PendingIntent pendingIntentClick = PendingIntent.getBroadcast(ctx, 0, clickintent, 0);
                views.setOnClickPendingIntent(R.id.widget_fondo_titulo, pendingIntentClick);

                Intent click2intent = new Intent(INTENT_GO_TO_NEW);
                PendingIntent pendingIntentClick2 = PendingIntent.getBroadcast(ctx, 0, click2intent, 0);
                views.setOnClickPendingIntent(R.id.widget_fondo_Texto, pendingIntentClick2);

                gm.updateAppWidget(thisWidget, views);
            }
            REFRESH_COUNT++;
        }

        handler.removeCallbacks(this);
        handler.postDelayed(this, WIDGET_REFRESH_TIME*1000);
    }   
};  

可运行在我的Widget类的方法的onUpdate最初推出的:

The runnable is initially launched in the onUpdate method of my Widget class:

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    ctx = context;
    context.startService(new Intent(context, UpdateWidgetService.class));   
    handler.postDelayed(updateWidgetText,WIDGET_REFRESH_TIME*1000);
// here goes some more code, updating the views and refreshing click intents
}

我把这个情况下,有人认为它有用。

I put this in case someone finds it useful.

不过,让我去开门见山地说:当我拿到手机出睡眠(打开屏幕),小部件就会发疯,并开始与快进样作用改变TextViews。
我假定这是因为有一些postDelayed事件队列,或者可以是在updateAppWidget队列。
我已经使用这里显示的code已经尝试过一种解决方法:的http://thinkandroid.word$p$pss.com/2010/01/24/handling-screen-off-and-screen-on-intents/
你可以看到它在我的code中的第一个片段,我检查已存储的屏幕状态下,当屏幕被关闭,以避免使用postDelayed一个布尔变量。但是,这似乎并没有解决它。

But let me go straight to the point: when I get the phone out of sleep (turn on the screen), the widget goes crazy and starts changing the TextViews with a fastforward-like effect. I assume it is because there are some postDelayed events in queue, or may be in the updateAppWidget queue. I've already tried a workaround using the code shown in here: http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ You can see it in the first snippet in my code, I check a boolean variable that has stored screen state to avoid using postDelayed when the screen is turned off. But that doesn't seem to fix it.

此问题现在已经快把我逼疯了一个星期,所以我问出绝望的:有没有办法做正确吗?

This problem is driving me crazy for one week now, so I ask out of despair: is there any way to do this properly?

推荐答案

您postDelayed电话是不是你的,如果(screenEnabled)块中,因此它会继续发布事件即使在screenEnabled是假的。

Your postDelayed call isn't inside your if (screenEnabled) block, so it will continue to post the events even when the screenEnabled is false

这篇关于自我更新每隔10秒小部件Handler.postDelayed问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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