Android Widget: Show configuration activity before

2019-03-25 02:44发布

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.

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.

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

  1. Widget.onRecive: action = APPWIDGET_ENABLED
  2. Widget.onEnabled
  3. Widget.onReceive: action = APPWIDGET_UPDATE
  4. Widget.onUpdate: Widget Service is started.
  5. WidgetService.onStartCommand: Potentially long running work which will delay the configuration activity from being immediately shown.
  6. WidgetConfiguration.onCreate
  7. Widget.onReceive: action = APPWIDGET_UPDATE
  8. Widget.onUpdate: Widget Service is started again
  9. WidgetService.onStartCommand: Potentially long running work is performed again.

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?

2条回答
Evening l夕情丶
2楼-- · 2019-03-25 03:09

Declare ActivityConfig in manifest:

<activity android:name="com.zoostudio.moneylover.widget.ActivityWidgetConfig" android:label="Hello Widget Config"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </intent-filter> </activity>

With update widget class:

public abstract class SampleWiget extends AppWidgetProvider {

}

follow android developer widget support to understand it.

查看更多
神经病院院长
3楼-- · 2019-03-25 03:13

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

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!

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.

查看更多
登录 后发表回答