Android Widget:在将小部件添加到屏幕之前显示配置活动

发布于 2025-01-02 16:43:23 字数 2356 浏览 0 评论 0原文

我有一个 Android Widget,它使用 Web 服务来检索并显示 Widget 上的数据。该小部件有一个扩展 PreferenceActivity 的配置活动。安装小部件后,配置活动就会启动,这是该小部件所需的行为。

问题是,每当将小部件添加到主屏幕时,小部件都会在配置活动启动/完成之前尝试更新自身,这可能会导致长时间延迟(几秒钟)。配置活动应该在每次添加新小部件时小部件尝试更新自身之前发生。

以下是添加小部件时我在 LogCat 中看到的事件序列:

  1. Widget.onRecive:action = APPWIDGET_ENABLED
  2. Widget.onEnabled
  3. Widget.onReceive:action = APPWIDGET_UPDATE
  4. Widget.onUpdate:小部件服务已启动。
  5. WidgetService.onStartCommand:可能长时间运行的工作,这将延迟配置活动的立即显示。
  6. WidgetConfiguration.onCreate
  7. Widget.onReceive:action = APPWIDGET_UPDATE
  8. Widget.onUpdate:再次启动Widget Service
  9. WidgetService.onStartCommand:再次执行可能长时间运行的工作。

发生的情况是,当添加小部件时,服务将在显示配置视图之前启动。

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>



问题
有没有办法在系统尝试将小部件添加到主屏幕之前强制显示配置活动?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2025-01-09 16:43:23

来自安卓文档:
http://developer.android.com/guide/topics/appwidgets/index .html#配置

创建App Widget时不会调用onUpdate()方法(启动配置Activity时系统不会发送ACTION_APPWIDGET_UPDATE广播)。首次创建 App Widget 时,配置 Activity 负责向 AppWidgetManager 请求更新。但是,后续更新将调用 onUpdate() - 仅在第一次时跳过。

然而,这似乎并不正确!

我所做的就是向 SharedPreferences 添加一个布尔值,它告诉我这个 widgetId 是否已经通过配置。如果没有跳过此更新。在 AppWidgetProvider 类的 onUpdate 方法中实现这一点。

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.

浅语花开 2025-01-09 16:43:23

在清单中声明 ActivityConfig:


<意图过滤器>
>

更新小部件类:

public abstract class SampleWiget extends AppWidgetProvider {

}

遵循 android 开发者小部件支持来理解它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文