01.public class NewsWidgetProvider extends AppWidgetProvider {
02.
03.
04. @Override
05. public void onDeleted(Context context, int[] appWidgetIds) {
06. }
07.
08. @Override
09. public void onDisabled(Context context) {
10. // TODO Auto-generated method stub
11. super.onDisabled(context);
12. }
13.
14. @Override
15. public void onEnabled(Context context) {
16. // TODO Auto-generated method stub
17. super.onEnabled(context);
18. }
19.
20. @Override
21. public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
22.
23. NewsService.updateAppWidgetIds(appWidgetIds);
24. context.startService(new Intent(context,NewsService.class));
25. }
26.
27. public static RemoteViews updateAppWidget(Context context,List<RssNews> list) {
28. RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget_layout);
29. if (list.size() > 3) {
30. views.setTextViewText(R.id.textView01, list.get(0).title);
31. views.setTextViewText(R.id.textView02, list.get(1).title);
32. views.setTextViewText(R.id.textView03, list.get(2).title);
33. }
34.
35. Intent detailIntent=new Intent(context,NewsSiteList.class);
36.
37. PendingIntent pending=PendingIntent.getActivity(context, 0, detailIntent, 0);
38. views.setOnClickPendingIntent(R.id.textView01, pending);
39. views.setOnClickPendingIntent(R.id.textView02, pending);
40. views.setOnClickPendingIntent(R.id.textView03, pending);
41. return views;
42. }
43.
44. @Override
45. public void onReceive(Context context, Intent intent) {
46. super.onReceive(context, intent);
47. }
48.
49.}
view plaincopy to clipboardprint?
01.public class NewsWidgetProvider extends AppWidgetProvider {
02.
03.
04. @Override
05. public void onDeleted(Context context, int[] appWidgetIds) {
06. }
07.
08. @Override
09. public void onDisabled(Context context) {
10. // TODO Auto-generated method stub
11. super.onDisabled(context);
12. }
13.
14. @Override
15. public void onEnabled(Context context) {
16. // TODO Auto-generated method stub
17. super.onEnabled(context);
18. }
19.
20. @Override
21. public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
22.
23. NewsService.updateAppWidgetIds(appWidgetIds);
24. context.startService(new Intent(context,NewsService.class));
25. }
26.
27. public static RemoteViews updateAppWidget(Context context,List<RssNews> list) {
28. RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget_layout);
29. if (list.size() > 3) {
30. views.setTextViewText(R.id.textView01, list.get(0).title);
31. views.setTextViewText(R.id.textView02, list.get(1).title);
32. views.setTextViewText(R.id.textView03, list.get(2).title);
33. }
34.
35. Intent detailIntent=new Intent(context,NewsSiteList.class);
36.
37. PendingIntent pending=PendingIntent.getActivity(context, 0, detailIntent, 0);
38. views.setOnClickPendingIntent(R.id.textView01, pending);
39. views.setOnClickPendingIntent(R.id.textView02, pending);
40. views.setOnClickPendingIntent(R.id.textView03, pending);
41. return views;
42. }
43.
44. @Override
45. public void onReceive(Context context, Intent intent) {
46. super.onReceive(context, intent);
47. }
48.
49.}
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代码
01.public class NewsService extends Service implements Runnable {
02.
03. private static final String TAG="NewsService";
04.
05. private static Queue<Integer> sAppWidgetIds=new LinkedList<Integer>();
06.
07. public static final String ACTION_UPDATE_ALL = "com.xxxx.news.UPDATE_ALL";
08.
09. private static boolean sThreadRunning = false;
10.
11. private static Object sLock = new Object();
12.
13.
14.
15. @Override
16. public IBinder onBind(Intent intent) {
17. return null;
18. }
19.
20. public static void updateAppWidgetIds(int[] appWidgetIds){
21. synchronized (sLock) {
22. for (int appWidgetId : appWidgetIds) {
23. sAppWidgetIds.add(appWidgetId);
24. }
25. }
26. }
27.
28. public static int getNextWidgetId(){
29. synchronized (sLock) {
30. if (sAppWidgetIds.peek() == null) {
31. return AppWidgetManager.INVALID_APPWIDGET_ID;
32. } else {
33. return sAppWidgetIds.poll();
34.
35. }
36. }
37. }
38.
39. private static boolean hasMoreUpdates() {
40. synchronized (sLock) {
41. boolean hasMore = !sAppWidgetIds.isEmpty();
42. if (!hasMore) {
43. sThreadRunning = false;
44. }
45. return hasMore;
46. }
47. }
48.
49. @Override
50. public void onCreate() {
51. super.onCreate();
52. }
53.
54. @Override
55. public void onStart(Intent intent, int startId) {
56. super.onStart(intent, startId);
57. if (null != intent) {
58. if (ACTION_UPDATE_ALL.equals(intent.getAction())) {
59. AppWidgetManager widget = AppWidgetManager.getInstance(this);
60. updateAppWidgetIds(widget.getAppWidgetIds(new ComponentName(this, NewsWidgetProvider.class)));
61. }
62. }
63. synchronized (sLock) {
64. if (!sThreadRunning) {
65. sThreadRunning=true;
66. new Thread(this).start();
67. }
68. }
69. }
70.
71. @Override
72. public void run() {
73. SharedPreferences setting=getSharedPreferences("com.xxxx.news_preferences", 0);
74. String updateTime=setting.getString("list_time", "1800000");
75. String updateUrl=setting.getString("list_site", "xxxxxxxxx");
76.
77.
78. AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(this);
79. RemoteViews updateViews=null;
80.
81. while (hasMoreUpdates()) {
82. int appWidgetId=getNextWidgetId();
83. List<RssNews> listNews = NewsContenttList.getNewsList(updateUrl);
84. if (listNews != null) {
85. updateViews = NewsWidgetProvider.updateAppWidget(this, listNews);
86. }
87. if (updateViews != null) {
88. appWidgetManager.updateAppWidget(appWidgetId, updateViews);
89. }
90. }
91.
92. Intent updateIntent=new Intent(ACTION_UPDATE_ALL);
93. updateIntent.setClass(this, NewsService.class);
94. PendingIntent pending=PendingIntent.getService(this, 0, updateIntent, 0);
95.
96. Time time = new Time();
97. long nowMillis = System.currentTimeMillis();
98. time.set(nowMillis+Long.parseLong(updateTime));
99. long updateTimes = time.toMillis(true);
100. Log.d(TAG, "request next update at "+updateTimes);
101.
102. AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
103. alarm.set(AlarmManager.RTC_WAKEUP, updateTimes, pending);
104. stopSelf();
105. }
106.
107.}