我试图创建一个定期更新的小部件,就像时钟小部件一样。窗口小部件无异常地加载和运行,但它没有明显的原因停止更新UI。当我将它连接到调试器时,没有抛出异常。而且没有任何模式。它随机停止。当它失去焦点时,它不会死亡(移动到不同的屏幕或新的应用程序出现)。小部件将在可见屏幕上运行,手机处于唤醒状态,并且只是停止更新。
我的猜测是操作系统正在进行清理并在需要资源时终止进程。有没有告诉操作系统保持我的进程活着?
private PendingIntent createUpdateIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(context, UpdateService.class));
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
super.onEnabled(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.setRepeating(alarmManager.RTC, calendar.getTimeInMillis(), 1000, createUpdateIntent(context));
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
super.onDisabled(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.cancel(createUpdateIntent(context));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
context.startService(new Intent(context, UpdateService.class));
}
}
public static class UpdateService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startID) {
RemoteViews updateViews = buildUpdate(this);
ComponentName widget = new ComponentName(this, MainWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(widget, updateViews);
}
private RemoteViews buildUpdate(Context context) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main_widget);
if (!EventStart.equals("")) {
DateTimeFormatter fmt = DateTimeFormat.forPattern("M/d/yyyy h:mm a");
endDate = fmt.parseDateTime(EventStart);
Calendar cal = Calendar.getInstance();
TimeZone UTC = TimeZone.getTimeZone("UTC");
cal.setTimeZone(UTC);
Date beginDate = cal.getTime();
Date dateEnd = endDate.toDate();
Map<TimeUnit,Long> result = computeDiff(
beginDate,
dateEnd
);
String Seconds = String.valueOf(result.get(TimeUnit.SECONDS));
if (result.get(TimeUnit.DAYS) <= 0 && result.get(TimeUnit.HOURS) <= 0 && result.get(TimeUnit.MINUTES) <= 0 && result.get(TimeUnit.SECONDS) <= 0) {
if (doVibrate) {
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
}
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.cancel(PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
updateViews.setTextViewText(R.id.info, Seconds);
return updateViews;
}
}