Android实现widget定时更新

在开发Android的widget时,第一个需要解决的问题就是怎么实现让widget定时更新,在查阅比较多的资料后,一般情况用Service在后台更新,废话不多说,贴代码如下: 

Widget: 

public class NewsWidgetProvider extends AppWidgetProvider {

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {

		NewsService.updateAppWidgetIds(appWidgetIds);
		context.startService(new Intent(context,NewsService.class));
	}
	
	public static RemoteViews updateAppWidget(Context context,List<RssNews> list) {
		RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget_layout);
		if (list.size() > 3) {
			views.setTextViewText(R.id.textView01, list.get(0).title);
			views.setTextViewText(R.id.textView02, list.get(1).title);
			views.setTextViewText(R.id.textView03, list.get(2).title);
		}

		Intent detailIntent=new Intent(context,NewsSiteList.class);
		
		PendingIntent pending=PendingIntent.getActivity(context, 0, detailIntent, 0);
		views.setOnClickPendingIntent(R.id.textView01, pending);
		views.setOnClickPendingIntent(R.id.textView02, pending);
		views.setOnClickPendingIntent(R.id.textView03, pending);
		return views;
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
	}

}

Service:

public class NewsService extends Service implements Runnable {
	
	private static final String TAG="NewsService"; 
	private static Queue<Integer> sAppWidgetIds=new LinkedList<Integer>();
	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() {
		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<RssNews> listNews = NewsContenttList.getNewsList(updateUrl);
			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();
	}
 }

本文转载自:http://www.oschina.net/question/5189_8939

你可能感兴趣的:(android,String,object,service,null,action)