简体   繁体   English

启动配置活动时调用Widget onUpdate

[英]Widget onUpdate called when Configuration Activity is launched

I'm implementing a widget and I'm facing the following problems: 我正在实现一个小部件,我面临以下问题:

1) onUpdate is called when I add the widget to the home screen, even if I specified a Configuration Activity. 1)当我将小部件添加到主屏幕时,即使我指定了配置活动,也会调用onUpdate As soon as I add it to the home screen, the APPWIDGET_ENABLED broadcast is sent, followed by the APPWIDGET_UPDATE and then the configuration activity is launched.. Is this a bug? 一旦我将其添加到主屏幕,就会发送APPWIDGET_ENABLED广播,然后发送APPWIDGET_UPDATE ,然后启动配置活动..这是一个错误吗? How should I understand in the onUpdate method that is being invoked before the configuration activity has returned? 我应该如何理解在配置活动返回之前调用的onUpdate方法? I can do it through a shared preference value, but I'd like it to behave as written on the developer guide, ie the onUpdate method should not be called. 我可以通过共享首选项值来实现,但我希望它的行为与开发人员指南中的内容相同,即不应该调用onUpdate方法。

2) onUpdate is not called every updatePeriodMillis seconds, which have been set to 10000 , ie 10 seconds for testing purposes.. Did I miss something in the receiver declaration within the Manifest file? 2) onUpdate没有被调用每个updatePeriodMillis秒,它已被设置为10000 ,即10秒用于测试目的..我是否错过了Manifest文件中receiver声明中的内容? I keep receiving the Lint warning Exported receiver does not require permission but I think this is a Lint issue and not my fault. 我一直收到Lint警告导出的接收器不需要许可,但我认为这是一个Lint问题而不是我的错。 EDIT : I've just found this within the reference docs: Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes. 编辑 :我刚刚在参考文档中找到了这个: 注意:updatePeriodMillis请求的更新不会每30分钟发送一次。 So it is correct that the widget is not updated how often I'd specified and I've changed the time to 1800000 milliseconds. 因此,我不会更新窗口小部件的频率是正确的,我将时间更改为1800000毫秒。

3) I want to deliver my own broadcast action to the widget provider, is it correct to add another receiver block in the Manifest targeting the same provider class or should I add only another intent action within the intent-filter that contains the APPWIDGET_UPDATE action? 3)我想将自己的广播操作传递给窗口小部件提供程序,在Manifest中添加另一个receiver块以定位相同的提供程序类是正确的还是我应该在包含APPWIDGET_UPDATE操作的intent-filter中添加另一个intent操作? BTW, I've commented my second receiver block and it is not the cause of the problems above. 顺便说一句,我评论了我的第二个receiver块,这不是上述问题的原因。 I created another receiver block because I wanted to declare it as not exported, in order to let the intent-filter action be triggered only by my app code and not anyone else. 我创建了另一个receiver块,因为我想将其声明为未导出,以便让intent-filter操作仅由我的应用程序代码而不是其他任何人触发。

AndroidManifest.xml AndroidManifest.xml中

    <receiver android:name="MyWidgetProvider" 
        android:exported="true">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/my_widget_info" />
    </receiver>
    <receiver android:name="MyWidgetProvider" 
        android:exported="false">
        <intent-filter>
            <action android:name="org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE" />
        </intent-filter>
    </receiver>

my_widget_info.xml my_widget_info.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="110dp"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/my_widget_layout"
    android:configure="org.test.mywidget.MyWidgetConfiguration" 
    android:resizeMode="none">
</appwidget-provider>

Providing an answer after digging into the source code: 在深入研究源代码后提供答案:

1) This is expected behavior see here 1)这是预期的行为,见这里

This method is also called when the user adds the App Widget 当用户添加App Widget时也会调用此方法

2) Seems you have found your own answer. 2)似乎你找到了自己的答案。 For others looking for the docs go here 寻找文档的其他人去这里

Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes 注意:updatePeriodMillis请求的更新不会每30分钟发送一次

3) Since the AppWidgetProvider extends BroadcastReceiver but is not declared final you can add the action from your second receiver 3)由于AppWidgetProvider扩展了BroadcastReceiver但未声明为final,您可以从第二个接收器添加动作

<receiver android:name="MyWidgetProvider" 
        android:exported="true">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/my_widget_info" />
    </receiver>

then you can override onReceive in your MyWidgetProvider class and if the action is your custom action handle it, otherwise call to the super.onRecieve(Context context, Intent intent) like so: 那么你可以覆盖你的MyWidgetProvider类中的onReceive,如果动作是你的自定义动作处理它,否则调用super.onRecieve(Context context,Intent intent),如下所示:

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction()
         .equals("org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE")){
        // handle your action
    } else {
        super.onRecieve(context, intent);
    }
}

as per the Guide: 根据指南:

AppWidgetProvider is just a convenience class. AppWidgetProvider只是一个方便的类。 If you would like to receive the App Widget broadcasts directly, you can implement your own BroadcastReceiver or override the onReceive(Context, Intent) callback. 如果您希望直接接收App Widget广播,您可以实现自己的BroadcastReceiver或覆盖onReceive(Context,Intent)回调。

One thing to note with this, the update to the remote view will only be called at the updatePeriodMillis, regardless of you adding your own action to the Intent for the provider to handle. 有一点需要注意,远程视图的更新只会在updatePeriodMillis上调用,无论您是否将自己的操作添加到Intent以供提供程序处理。

Good Luck and Happy Coding! 祝你好运,快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM