配置了Android部件:配置将收到的onupdate即使配置未完成未完成、部件、Android、onupdate

2023-09-06 13:52:13 作者:帥の冭濄衯

这是我如何配置我的应用程序的窗口小部件:

This is how I configure my APP's widget:

<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="240dp"
    android:minHeight="193dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/xyz_appwidget"
    android:configure="com.xyz.activity.Configuration"
    />

因此​​,系统将弹出配置的活动,一旦小部件已经被提上了主屏幕的用户。不幸的是,没有配置应用,该插件不应该把在屏幕上。

Therefore, the system will bring up the Configuration activity, once the widget has been put on the homescreen by the user. Unfortunately, without the configuration applied, this widget should not be put on the screen.

我加了一些调试输出。这发生在用户正在选择窗口小部件为他的主屏幕。 注意:这是当配置活动是在前面,而不是小工具

I added some debugging output. This happens when the user is selecting the widget for his homescreen. Note: This is when the Configuration activity is in front, NOT the widget.

I/ActivityManager(  101): Start proc com.xyz for broadcast com.xyz/.widget.xyz.XyzWidgetProvider: pid=14371 uid=10050 gids={3003, 1015}
D/XyzWidgetProvider(14371): onReceive: android.appwidget.action.APPWIDGET_ENABLED
D/XyzWidgetProvider(14371): onEnabled
D/XyzWidgetProvider(14371): onReceive: android.appwidget.action.APPWIDGET_UPDATE

所以,即使是小部件没有配置意味着,这两个事件(启用,UPDATE)将被解雇。 启用对我来说很有意义,但更新显然不是。特别是当配置活动顺利完成,不需要额外的更新事件被发送。

So that means even the widget is not configured, both events (ENABLED, UPDATE) will be fired. Enabled makes sense to me, but UPDATE clearly not. Especially when the Configuration activity finished successfully, no additional UPDATE event is being sent.

我也读到了配置活动的结果设置为 Activity.RESULT_CANCELED ,一个是能够取消配置过程。但是,因为这显然异步运行,我不知道如何使配置过程的阻止?任何人都遇到过这个?

I also read that by setting the result of the Configuration activity to Activity.RESULT_CANCELED, one is able to cancel the configuration process. But as this clearly runs asynchronous I don't know how to make the configuration process blocking? Anyone encountered this before?

我的最后一点:我曾在Android画廊的Widget一看,里面的不知何故的管理这个正常,因此未配置图库部件将不会被添加。但我怀疑一些HTC的魔术的存在作为日志不是非常有帮助。

My final note: I had a look in the Android Gallery Widget, which somehow manages this properly, so a non-configured gallery widget will not be added. But I suspect some HTC magic there as the logs are not very helpful.

推荐答案

下面是你如何处理这个问题: 在您的配置活动,这样在您的onCreate(): 的setResult(Activity.RESULT_CANCELED);

Here is how you handle this: In your configuration activity, do this in your onCreate(): setResult(Activity.RESULT_CANCELED);

在配置活动完成后,例如所有参数设置和窗口小部件应显示,做 的setResult(Activity.RESULT_OK);

Once the configuration activity is done, e.g. all parameters are set and widget should be shown, do setResult(Activity.RESULT_OK);

因此​​,如果用户不完成配置活动,小部件被去除。

So, if the user does not finish the configuration activity, your widget is removed.

不过,在小部件code本身(的onupdate),你还需要检查的配置是否完整,因为APPWIDGET_UPDATE肯定会配置完成前调用。你可以只跳过所有更新完全如果配置不存在,你的小工具将不会在这一点上可见无论如何,在完成配置后,小部件或者由系统中删除,或它的配置是存在的。

However, in the widget code itself (onUpdate), you still need to check whether configuration is complete, because APPWIDGET_UPDATE will definitely be called before configuration completion. You can just skip all updating altogether if configuration is not there, your widget will not be visible at this point anyway, and once configuration is complete, your widget is either removed by the system or its configuration is there.

一件事。你需要从成功完成后您配置活动启动窗口更新,因为系统本身不会做。

One more thing. You need to initiate widget update from your configuration activity upon successful completion, because the system will not do it by itself.

 
精彩推荐
图片推荐