Android实现widget定时更新
2010-06-19 17:37:00 来源:WEB开发网在开发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
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);
更多精彩
赞助商链接