我的应用程序小部件从不转到 onUpdate 函数 我添加了 3000 mili 用于测试,但除了第一次添加它之外, 它永远不会进入 onUpdate 函数。
我做错了什么?
我的 list 文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="layout.ClockWidgetClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/clock_widget_class_info" />
</receiver>
</application>
我的时钟小部件:
/**
* Implementation of App Widget functionality.
*/
public class ClockWidgetClass extends AppWidgetProvider {
static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
//getting shered prefrences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String name = preferences.getString("randomString", "");
if(!name.equalsIgnoreCase(""))
{
name = name; /* Edit the value here*/
}
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_class);
//set chosen design visible
//set all visibility to 0
views.setViewVisibility(R.id.AnalogClock0, View.INVISIBLE);
views.setViewVisibility(R.id.AnalogClock1, View.INVISIBLE);
views.setViewVisibility(R.id.AnalogClock2, View.INVISIBLE);
views.setViewVisibility(R.id.AnalogClock3, View.INVISIBLE);
//turning on the correct clock
if (name.equals("1")) {
views.setViewVisibility(R.id.AnalogClock0, View.VISIBLE);
} else if (name.equals("2")) {
views.setViewVisibility(R.id.AnalogClock1, View.VISIBLE);
} else if (name.equals("3")) {
views.setViewVisibility(R.id.AnalogClock2, View.VISIBLE);
} else {
views.setViewVisibility(R.id.AnalogClock3, View.VISIBLE);
}
// views.setTextViewText(R.id.appwidget_text, name);
//updateting the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
@Override
public void onReceive(Context context, Intent intent) {
// Chain up to the super class so the onEnabled, etc callbacks get dispatched
super.onReceive(context, intent);
// Handle a different Intent
Log.d("reciving", "onReceive()" + intent.getAction());
}
}
我的小部件 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/clock_widget_class"
android:initialLayout="@layout/clock_widget_class"
android:minHeight="110dp"
android:minWidth="250dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="3000"
android:widgetCategory="home_screen"></appwidget-provider>
最佳答案
Never 可能不正确。它只是没有在您期望它发生的时间发生。随着documentation for updatePeriodMillis
你会发现一个重要提示:
Note: Updates requested with
updatePeriodMillis
will not be delivered more than once every 30 minutes.
因此您可能需要等待 30 分钟才能再次调用 onUpdate()
。
关于android - appwidget 从不去 onUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317694/