机器人的Widget:小窗口之前显示配置活动被添加到屏幕机器人、屏幕、窗口、Widget

2023-09-06 13:54:38 作者:海绵宝宝的微笑我做不到

我有一个使用Web服务来检索和显示的插件的数据一个Android窗口小部件。窗口小部件有一个扩展preferenceActivity A配置活动。配置活动启动只要插件被安装,这是该插件的期望的行为。

I have an Android Widget that uses web services to retrieve and display the data on the widget. The widget has a configuration activity that extends PreferenceActivity. The configuration activity starts up as soon as the widget is installed, which is the desired behavior for this widget.

的问题是,当一个部件被添加到主屏幕,窗口小部件尝试更新iteself配置活动被启动/完成这可能潜在地导致长时间的延迟(几秒钟)之前。小部件随时尝试一个新的widget被加到自我更新前的配置活动应该发生。

The problem is, whenever a widget is added to the home screen, the widget attempts to update iteself before the configuration activity is started/completed which may potentially lead to a long delay (several seconds). The configuration activity should occur before the widget attempts to update itself anytime a new widget is added.

下面是我看到的LogCat中,当一个widget被加到事件序列:

Here is the sequence of events that I'm seeing in LogCat when a widget is added:

Widget.onRecive:动作= APPWIDGET_ENABLED Widget.onEnabled Widget.onReceive:动作= APPWIDGET_UPDATE Widget.onUpdate:小工具服务已启动 WidgetService.onStartCommand:潜在的长期运行,这将延迟配置活动被立即显示工作 WidgetConfiguration.onCreate Widget.onReceive:动作= APPWIDGET_UPDATE Widget.onUpdate:小工具服务将再次启动 WidgetService.onStartCommand:潜在的长时间运行的工作再次进行

发生了什么事是,当一个小部件添加,该服务将启动配置视图中已被证明了。

What's happening is that when a widget is added, the service will start up before the configuration view has been shown.

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx.xxx.xxxwidget"
    android:versionCode="1"
    android:versionName="@string/app_version" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="xxxWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

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

        <activity android:name="xxxWidgetConfigure" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="xxxWidgetService" />
    </application>

</manifest>

问题 有没有一种方法,系统尝试将小工具添加到主屏幕之前强制要显示的配置活动?

Question Is there a way to force the configuration activity to be shown before the system attempts to add the widget to the home screen?

推荐答案

从Android电子文档: http://developer.android.com/guide/topics/appwidgets/index.html#Configuring

From android documentation: http://developer.android.com/guide/topics/appwidgets/index.html#Configuring

在应用程序创建Widget(系统不会发送ACTION_APPWIDGET_UPDATE广播时,配置活动启动)的的OnUpdate()方法将不会被调用。这是配置活动的责任,第一次创建的应用程序窗口小部件时从AppWidgetManager请求更新。然而,的OnUpdate()将呼吁后续更新,它只是跳过第一次。

The onUpdate() method will not be called when the App Widget is created (the system will not send the ACTION_APPWIDGET_UPDATE broadcast when a configuration Activity is launched). It is the responsibility of the configuration Activity to request an update from the AppWidgetManager when the App Widget is first created. However, onUpdate() will be called for subsequent updates—it is only skipped the first time.

不过,这似乎并不正确!

HOWEVER, this does not seem to be correct!

我所做的就是添加一个布尔值,共享preferences它告诉我,如果这为widgetid已经通过配置。如果不能跳过此更新。在你的AppWidgetProvider类的实现此的onupdate方法。

What I did was adding a boolean to SharedPreferences which tells me if this widgetId has been through configuration. If not skip this update. Implement this in your AppWidgetProvider class' onUpdate method.

 
精彩推荐