Android实现widget定时更新
2010-06-19 17:37:00 来源:WEB开发网SharedPreferences setting=getSharedPreferences("com.xxxx.news_preferences", 0);
String updateTime=setting.getString("list_time", "1800000");
String updateUrl=setting.getString("list_site", "xxxxxxxxx");
AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(this);
RemoteViews updateViews=null;
while (hasMoreUpdates()) {
int appWidgetId=getNextWidgetId();
List
if (listNews != null) {
updateViews = NewsWidgetProvider.updateAppWidget(this, listNews);
}
if (updateViews != null) {
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
}
Intent updateIntent=new Intent(ACTION_UPDATE_ALL);
updateIntent.setClass(this, NewsService.class);
PendingIntent pending=PendingIntent.getService(this, 0, updateIntent, 0);
Time time = new Time();
long nowMillis = System.currentTimeMillis();
time.set(nowMillis+Long.parseLong(updateTime));
long updateTimes = time.toMillis(true);
Log.d(TAG, "request next update at "+updateTimes);
AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, updateTimes, pending);
stopSelf();
}
}
public class NewsService extends Service implements Runnable {
private static final String TAG="NewsService";
private static Queue
public static final String ACTION_UPDATE_ALL = "com.xxxx.news.UPDATE_ALL";
private static boolean sThreadRunning = false;
private static Object sLock = new Object();
@Override
public IBinder onBind(Intent intent) {
赞助商链接