当前位置:操作系统 > 安卓/Android >>

Android实现widget定时更新

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


Widget:

 

[Java] 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);

}

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:


[Java] 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(); 
                                                                                                                                                                                                                            

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);

}<

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,