通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示一小会儿,这是什么问题? [英] Power control widget shown for a short moment when updating my own widget via AppWidgetManager, what's the problem?

查看:28
本文介绍了通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示一小会儿,这是什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 AppWidgetManager.updateAppWidget 手动更新小部件时遇到问题.平台为 Android 2.2.

I've got a problem when updating my widget manually via AppWidgetManager.updateAppWidget. Platform is Android 2.2.

代码如下:
我在清单中的现有活动中另外声明了小部件:

Here's the code:
I declared the widget additionally to an existing Activity in the Manifest:

<receiver android:name=".Widget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>

小部件类在 Widget.java 中声明:

The widget-class was declared in Widget.java:

public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    int use_static_ip;
    RemoteViews remoteViews;

    try {
        use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
        if (use_static_ip == 0) { //DHCP
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
        } else { //static IP
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
        }
        Intent call_activity = new Intent(context, StaticIPToggle.class);
        PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
        remoteViews.setOnClickPendingIntent(R.id.widget_icon, pending_call_activity);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    } catch (SettingNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

在现有活动中,我添加了一些行来手动更新小部件:

In the existing activity I added some lines to manually update the widget:

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        ComponentName componentName = new ComponentName(getApplicationContext(), Widget.class);
        int[] ids = appWidgetManager.getAppWidgetIds(componentName);
        Intent update_widget = new Intent();
        update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
        update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        getApplicationContext().sendBroadcast(update_widget);

但现在我有这个错误:如果小部件是 onClicked,它会在显示所需小部件之前短时间(~0,5sec-1sec)显示能量设置小部件.

But now I have this bug: If the widget is onClicked, it shows the energy-settings-widget for a short period (~0,5sec-1sec) before the desired widget is shown.

这里是这个问题的截图(从左到右的顺序):http://i.stack.imgur.com/rHkjw.jpg

Here a screenshot of this issue (sequence from left to right): http://i.stack.imgur.com/rHkjw.jpg

首先是左图中的小部件,然后变为中间图片,并在大约 0.5-1 秒后停留在那里或变为右图.

First there's the widget as in the left picture, then changes to the middle picture and either stays there or changes to the right picture after ~0,5-1sec.

很遗憾,我在代码中看不到任何错误,是吗?

Unfortunately I can't see any mistake in the code, do you?

希望你能帮我解决这个问题;)提前致谢,问候,奥利

Hopefully you can help me to fix this problem ;) Thanks in advance, regards, Oli

推荐答案

我使用与您相同的编程方式手动更新小部件(来自 是否有可能以编程方式抛出 APPWIDGET_UPDATE 的意图?) 并发现这是问题的确切原因.

I used the same programatic way to manually update the widget as you did (From Is it possible to throw an intent for APPWIDGET_UPDATE programmatically?) and found this to be the exact cause of the issue.

这仍然会在 4.2 和 2.3、官方和非官方 ROMS 上发生(当这种情况发生时,Touchwiz 会变得非常疯狂).

This still happens on 4.2 and 2.3, Official and Unofficial ROMS (Touchwiz goes absolutely wild when this happens).

我可以解决此问题的唯一方法是删除涉及广播的整个更新,并通过向 Widget Provider 发送常规 Broadcast 并调用来远程更新小部件

The only way I could fix this was to remove the whole update involving the broadcast and to update the widget remotely by sending a regular Broadcast to the Widget Provider and calling

ComponentName thisWidget = new ComponentName(context,AvailabilityWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,views);

来自 onReceive

(这对我有用,因为我的小部件是相同的)

(This works for me as the my widgets are identical)

这样,问题似乎不会发生,我可以远程更新小部件.老问题,我知道,但值得回答,因为这是我能找到的关于这个奇怪问题的全部内容.

This way, the issue seemed to not occur and I could update the widget remotely. Old question, I know, but deserves an answer as this was all I could find on this weird issue.

这篇关于通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示一小会儿,这是什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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