我的应用程序小部件永远不要转到onUpdate函数 我添加了3000米的测试,但除了你第一次添加它, 它永远不会进入onUpdate函数。
我做错了什么?
我的清单文件:
<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>
我的clockWidget:
/**
* 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());
}
}
我的Widget 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>
答案 0 :(得分:3)
永远可能不正确。它不会发生在您期望它发生的时间。使用documentation for updatePeriodMillis
,您会找到一个重要的注释:
注意:
updatePeriodMillis
请求的更新不会每30分钟发送一次。
因此,您可能需要等待30分钟才能再次拨打onUpdate()
。