安卓小工具 : Show configuration activity before widget is added to the screen

标签 android android-widget android-manifest sharedpreferences android-preferences

我有一个 Android 小部件,它使用网络服务来检索和显示小部件上的数据。该小部件有一个扩展PreferenceActivity 的配置 Activity 。一旦安装了小部件,配置 Activity 就会启动,这是该小部件所需的行为。

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

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

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

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

list .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>



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

最佳答案

来自安卓文档: 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.

然而,这似乎不正确!

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

关于安卓小工具 : Show configuration activity before widget is added to the screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9144262/

相关文章:

android - 在 AppWidget Provider 中获取首选项

android - 0 台设备支持的应用

android - 试图获取下载文件大小,但出现错误

Android:如何从 WebView 中选择文本

android - 应用程序 (22.0.0) 和测试应用程序 (21.0.3) 的已解决版本不同

Android DatePickerDialog - 因 IllegalArgumentException 而失败

android - java.lang.RuntimeException : Unable to Instantiate activity ComponentInfo

java - 如何在 Espresso 中允许繁忙的工作线程

android - Flutter AndroidManifest 错误

android - 应用程序已安装并有两个图标