我的活动包含从互联网检索到的数据。我希望活动每5分钟自动刷新一次其内容。实现它的最佳方式是什么?我应该使用java的定时器和TimerTask吗?
谢谢。
发布于 2010-07-09 20:21:04
你需要设置一个定期触发的警报,一旦警报触发(广播接收器),确保它在你的适配器上调用notifiyDataSet,这样系统就会自动重建你的列表视图(如果你正在使用列表视图)
这是一个示例代码,用于设置警报,使其在X分钟后触发
Intent intent = new Intent(this, WeatherRefreshService.class);
PendingIntent sender = PendingIntent.getService(this, 0, intent, 0);
// We want the alarm to go off 60 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += REPEATING_ALARM_INTERVAL_IN_MINUTES * 60 * 1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
REPEATING_ALARM_INTERVAL_IN_MINUTES * 60 * 1000, sender);
此示例使用服务的PendingIntent,但如果您喜欢,也可以将其更改为广播。
https://stackoverflow.com/questions/3215949
复制