My widget stops updating/working after some time. I have a simple widget which displays quotes every few hours (now is set to seconds for testing), for timing it uses a separate thread.
What can be wrong? Why does it stop updating? Thanks!
package com.example.napiige;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends AppWidgetProvider {
String azIge;
static MainActivity[] ige = new MainActivity[3];
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
String ig1 = context.getString(R.string.ige1);
String ig2 = context.getString(R.string.ige2);
String ig3 = context.getString(R.string.ige3);
ige = new MainActivity[3];
ige[0] = new MainActivity();
ige[1] = new MainActivity();
ige[2] = new MainActivity();
ige[0].azIge = ig1;
ige[1].azIge = ig2;
ige[2].azIge = ig3;
go(context, appWidgetManager, appWidgetIds);
}
}
void go(final Context context, AppWidgetManager mgr, final int[] myWidgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
new Thread(new Runnable() {
public void run() {
int i = 0;
while (i < 3) {
try {
Thread.sleep(3000);
views.setTextViewText(R.id.igePrint, ige[i].azIge);
AppWidgetManager awManager = AppWidgetManager.getInstance(context);
awManager.updateAppWidget(myWidgetId, views);
} catch (InterruptedException e) {
e.printStackTrace();
}
i = i + 1;
if (i == 3) {
i = 0;
}
}
}
}).start();
}
}
If you're updating every few hours why not use the built in functionality to update the app widget found in AppWidgetProviderInfo, use the android:updatePeriodMillis
to set the update time (The minimum time you can get with this is 30 minutes I believe, but it should work for you). More information can be found at http://developer.android.com/guide/topics/appwidgets/index.html?utm_source=Android+Weekly&utm_campaign=b32a249c8f-Android_Weekly_46&utm_medium=email
I believe the system is killing your Thread when it feels like it, it is under no obligation to keep it running.
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/preview"
android:initialLayout="@layout/example_appwidget"
android:configure="com.example.android.ExampleAppWidgetConfigure"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard"
android:initialKeyguardLayout="@layout/example_keyguard">
</appwidget-provider>
如果设备在需要更新时处于睡眠状态,则设备将被唤醒以执行更新。即使您在android:updatePeriodMillis
了少于30分钟的测试目的android:updatePeriodMillis
它也不会更新,因为它需要分钟的时间为O update.we也可以手动调用reiever和通过检查的动作更新30个miintes receiever
和呼叫更新方法手动因为onUpdate()
梅托德不会被机器人调用
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.