Android Widget:在将小部件添加到屏幕之前显示配置活动
我有一个 Android Widget,它使用 Web 服务来检索并显示 Widget 上的数据。该小部件有一个扩展 PreferenceActivity
的配置活动。安装小部件后,配置活动就会启动,这是该小部件所需的行为。
问题是,每当将小部件添加到主屏幕时,小部件都会在配置活动启动/完成之前尝试更新自身,这可能会导致长时间延迟(几秒钟)。配置活动应该在每次添加新小部件时小部件尝试更新自身之前发生。
以下是添加小部件时我在 LogCat 中看到的事件序列:
- Widget.onRecive:action = APPWIDGET_ENABLED
- Widget.onEnabled
- Widget.onReceive:action = APPWIDGET_UPDATE
- Widget.onUpdate:小部件服务已启动。
- WidgetService.onStartCommand:可能长时间运行的工作,这将延迟配置活动的立即显示。
- WidgetConfiguration.onCreate
- Widget.onReceive:action = APPWIDGET_UPDATE
- Widget.onUpdate:再次启动Widget Service
- 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:
- Widget.onRecive: action = APPWIDGET_ENABLED
- Widget.onEnabled
- Widget.onReceive: action = APPWIDGET_UPDATE
- Widget.onUpdate: Widget Service is started.
- WidgetService.onStartCommand: Potentially long running work which will delay the configuration activity from being immediately shown.
- WidgetConfiguration.onCreate
- Widget.onReceive: action = APPWIDGET_UPDATE
- Widget.onUpdate: Widget Service is started again
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自安卓文档:
http://developer.android.com/guide/topics/appwidgets/index .html#配置
然而,这似乎并不正确!
我所做的就是向 SharedPreferences 添加一个布尔值,它告诉我这个 widgetId 是否已经通过配置。如果没有跳过此更新。在 AppWidgetProvider 类的 onUpdate 方法中实现这一点。
From android documentation:
http://developer.android.com/guide/topics/appwidgets/index.html#Configuring
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.
在清单中声明 ActivityConfig:
<意图过滤器>
更新小部件类:
}
遵循 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:
}
follow android developer widget support to understand it.