Android实现widget定时更新
2010-06-19 17:37:00 来源:WEB开发网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) {
return null;
}
public static void updateAppWidgetIds(int[] appWidgetIds){
synchronized (sLock) {
for (int appWidgetId : appWidgetIds) {
sAppWidgetIds.add(appWidgetId);
}
}
}
public static int getNextWidgetId(){
synchronized (sLock) {
if (sAppWidgetIds.peek() == null) {
return AppWidgetManager.INVALID_APPWIDGET_ID;
} else {
return sAppWidgetIds.poll();
}
}
}
private static boolean hasMoreUpdates() {
synchronized (sLock) {
boolean hasMore = !sAppWidgetIds.isEmpty();
if (!hasMore) {
sThreadRunning = false;
}
return hasMore;
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (null != intent) {
if (ACTION_UPDATE_ALL.equals(intent.getAction())) {
AppWidgetManager widget = AppWidgetManager.getInstance(this);
updateAppWidgetIds(widget.getAppWidgetIds(new ComponentName(this, NewsWidgetProvider.class)));
}
}
synchronized (sLock) {
if (!sThreadRunning) {
sThreadRunning=true;
new Thread(this).start();
}
}
}
@Override
public void run() {
更多精彩
赞助商链接