我在使用小部件时看到奇怪的行为。我确信它以前曾起作用,但无论如何现在都没有。不知道我做了什么更改或它是否确实有效。下面的代码创建了我正在尝试做的简化版本。一个应用程序提供一个小部件。当它放在屏幕上时,提示用户输入数字。然后,该编号将显示在窗口小部件上。单击小部件将数字打印到日志中。可以使用不同的编号创建此窗口小部件的多个实例。单击每个将产生一个与为每个小部件选择的编号相对应的日志条目。
但是,我看到某种缓存行为。假设我创建了widget 3和5。单击3将记录3。单击5将记录3。删除3并单击5将再次记录3。这是如何运作的?看起来第一个窗口小部件已创建并且其视图已缓存?
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.app">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".AppApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.DashboardActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.DummyWidgetConfigActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<receiver android:name=".DummyWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/dummy_widget_info" />
</receiver>
<service
android:name=".service.DummyWidgetService"
android:exported="false" />
</application> …Run Code Online (Sandbox Code Playgroud) android android-widget android-service android-activity android-pendingintent