Android实现widget定时更新

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

Widget:

publicclassNewsWidgetProviderextendsAppWidgetProvider{

@Override

publicvoidonDeleted(Contextcontext,int[]appWidgetIds){

}

@Override

publicvoidonDisabled(Contextcontext){

//TODOAuto-generatedmethodstub

super.onDisabled(context);

}

@Override

publicvoidonEnabled(Contextcontext){

//TODOAuto-generatedmethodstub

super.onEnabled(context);

}

@Override

publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,int[]appWidgetIds){

NewsService.updateAppWidgetIds(appWidgetIds);

context.startService(newIntent(context,NewsService.class));

}

publicstaticRemoteViewsupdateAppWidget(Contextcontext,List<RssNews>list){

RemoteViewsviews=newRemoteViews(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);

}

IntentdetailIntent=newIntent(context,NewsSiteList.class);

PendingIntentpending=PendingIntent.getActivity(context,0,detailIntent,0);

views.setOnClickPendingIntent(R.id.textView01,pending);

views.setOnClickPendingIntent(R.id.textView02,pending);

views.setOnClickPendingIntent(R.id.textView03,pending);

returnviews;

}

@Override

publicvoidonReceive(Contextcontext,Intentintent){

super.onReceive(context,intent);

}

}

Service:

publicclassNewsServiceextendsServiceimplementsRunnable{

privatestaticfinalStringTAG="NewsService";

privatestaticQueue<Integer>sAppWidgetIds=newLinkedList<Integer>();

publicstaticfinalStringACTION_UPDATE_ALL="com.xxxx.news.UPDATE_ALL";

privatestaticbooleansThreadRunning=false;

privatestaticObjectsLock=newObject();

@Override

publicIBinderonBind(Intentintent){

returnnull;

}

publicstaticvoidupdateAppWidgetIds(int[]appWidgetIds){

synchronized(sLock){

for(intappWidgetId:appWidgetIds){

sAppWidgetIds.add(appWidgetId);

}

}

}

publicstaticintgetNextWidgetId(){

synchronized(sLock){

if(sAppWidgetIds.peek()==null){

returnAppWidgetManager.INVALID_APPWIDGET_ID;

}else{

returnsAppWidgetIds.poll();

}

}

}

privatestaticbooleanhasMoreUpdates(){

synchronized(sLock){

booleanhasMore=!sAppWidgetIds.isEmpty();

if(!hasMore){

sThreadRunning=false;

}

returnhasMore;

}

}

@Override

publicvoidonCreate(){

super.onCreate();

}

@Override

publicvoidonStart(Intentintent,intstartId){

super.onStart(intent,startId);

if(null!=intent){

if(ACTION_UPDATE_ALL.equals(intent.getAction())){

AppWidgetManagerwidget=AppWidgetManager.getInstance(this);

updateAppWidgetIds(widget.getAppWidgetIds(newComponentName(this,NewsWidgetProvider.class)));

}

}

synchronized(sLock){

if(!sThreadRunning){

sThreadRunning=true;

newThread(this).start();

}

}

}

@Override

publicvoidrun(){

SharedPreferencessetting=getSharedPreferences("com.xxxx.news_preferences",0);

StringupdateTime=setting.getString("list_time","1800000");

StringupdateUrl=setting.getString("list_site","xxxxxxxxx");

AppWidgetManagerappWidgetManager=AppWidgetManager.getInstance(this);

RemoteViewsupdateViews=null;

while(hasMoreUpdates()){

intappWidgetId=getNextWidgetId();

List<RssNews>listNews=NewsContenttList.getNewsList(updateUrl);

if(listNews!=null){

updateViews=NewsWidgetProvider.updateAppWidget(this,listNews);

}

if(updateViews!=null){

appWidgetManager.updateAppWidget(appWidgetId,updateViews);

}

}

IntentupdateIntent=newIntent(ACTION_UPDATE_ALL);

updateIntent.setClass(this,NewsService.class);

PendingIntentpending=PendingIntent.getService(this,0,updateIntent,0);

Timetime=newTime();

longnowMillis=System.currentTimeMillis();

time.set(nowMillis+Long.parseLong(updateTime));

longupdateTimes=time.toMillis(true);

Log.d(TAG,"requestnextupdateat"+updateTimes);

AlarmManageralarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);

alarm.set(AlarmManager.RTC_WAKEUP,updateTimes,pending);

stopSelf();

}

}

相关推荐