Android实现widget定时更新

系统 1620 0

Java代码 复制代码 收藏代码
  1. public class NewsWidgetProvider extends AppWidgetProvider{
  2. @Override
  3. public void onDeleted(Contextcontext, int []appWidgetIds){
  4. }
  5. @Override
  6. public void onDisabled(Contextcontext){
  7. //TODOAuto-generatedmethodstub
  8. super .onDisabled(context);
  9. }
  10. @Override
  11. public void onEnabled(Contextcontext){
  12. //TODOAuto-generatedmethodstub
  13. super .onEnabled(context);
  14. }
  15. @Override
  16. public void onUpdate(Contextcontext,AppWidgetManagerappWidgetManager, int []appWidgetIds){
  17. NewsService.updateAppWidgetIds(appWidgetIds);
  18. context.startService( new Intent(context,NewsService. class ));
  19. }
  20. public static RemoteViewsupdateAppWidget(Contextcontext,List<RssNews>list){
  21. RemoteViewsviews= new RemoteViews(context.getPackageName(),R.layout.appwidget_layout);
  22. if (list.size()> 3 ){
  23. views.setTextViewText(R.id.textView01,list.get( 0 ).title);
  24. views.setTextViewText(R.id.textView02,list.get( 1 ).title);
  25. views.setTextViewText(R.id.textView03,list.get( 2 ).title);
  26. }
  27. IntentdetailIntent= new Intent(context,NewsSiteList. class );
  28. PendingIntentpending=PendingIntent.getActivity(context, 0 ,detailIntent, 0 );
  29. views.setOnClickPendingIntent(R.id.textView01,pending);
  30. views.setOnClickPendingIntent(R.id.textView02,pending);
  31. views.setOnClickPendingIntent(R.id.textView03,pending);
  32. return views;
  33. }
  34. @Override
  35. public void onReceive(Contextcontext,Intentintent){
  36. super .onReceive(context,intent);
  37. }
  38. }
    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代码 复制代码 收藏代码
  1. public class NewsService extends Service implements Runnable{
  2. private static final StringTAG= "NewsService" ;
  3. private static Queue<Integer>sAppWidgetIds= new LinkedList<Integer>();
  4. public static final StringACTION_UPDATE_ALL= "com.xxxx.news.UPDATE_ALL" ;
  5. private static boolean sThreadRunning= false ;
  6. private static ObjectsLock= new Object();
  7. @Override
  8. public IBinderonBind(Intentintent){
  9. return null ;
  10. }
  11. public static void updateAppWidgetIds( int []appWidgetIds){
  12. synchronized (sLock){
  13. for ( int appWidgetId:appWidgetIds){
  14. sAppWidgetIds.add(appWidgetId);
  15. }
  16. }
  17. }
  18. public static int getNextWidgetId(){
  19. synchronized (sLock){
  20. if (sAppWidgetIds.peek()== null ){
  21. return AppWidgetManager.INVALID_APPWIDGET_ID;
  22. } else {
  23. return sAppWidgetIds.poll();
  24. }
  25. }
  26. }
  27. private static boolean hasMoreUpdates(){
  28. synchronized (sLock){
  29. boolean hasMore=!sAppWidgetIds.isEmpty();
  30. if (!hasMore){
  31. sThreadRunning= false ;
  32. }
  33. return hasMore;
  34. }
  35. }
  36. @Override
  37. public void onCreate(){
  38. super .onCreate();
  39. }
  40. @Override
  41. public void onStart(Intentintent, int startId){
  42. super .onStart(intent,startId);
  43. if ( null !=intent){
  44. if (ACTION_UPDATE_ALL.equals(intent.getAction())){
  45. AppWidgetManagerwidget=AppWidgetManager.getInstance( this );
  46. updateAppWidgetIds(widget.getAppWidgetIds( new ComponentName( this ,NewsWidgetProvider. class )));
  47. }
  48. }
  49. synchronized (sLock){
  50. if (!sThreadRunning){
  51. sThreadRunning= true ;
  52. new Thread( this ).start();
  53. }
  54. }
  55. }
  56. @Override
  57. public void run(){
  58. SharedPreferencessetting=getSharedPreferences( "com.xxxx.news_preferences" , 0 );
  59. StringupdateTime=setting.getString( "list_time" , "1800000" );
  60. StringupdateUrl=setting.getString( "list_site" , "xxxxxxxxx" );
  61. AppWidgetManagerappWidgetManager=AppWidgetManager.getInstance( this );
  62. RemoteViewsupdateViews= null ;
  63. while (hasMoreUpdates()){
  64. int appWidgetId=getNextWidgetId();
  65. List<RssNews>listNews=NewsContenttList.getNewsList(updateUrl);
  66. if (listNews!= null ){
  67. updateViews=NewsWidgetProvider.updateAppWidget( this ,listNews);
  68. }
  69. if (updateViews!= null ){
  70. appWidgetManager.updateAppWidget(appWidgetId,updateViews);
  71. }
  72. }
  73. IntentupdateIntent= new Intent(ACTION_UPDATE_ALL);
  74. updateIntent.setClass( this ,NewsService. class );
  75. PendingIntentpending=PendingIntent.getService( this , 0 ,updateIntent, 0 );
  76. Timetime= new Time();
  77. long nowMillis=System.currentTimeMillis();
  78. time.set(nowMillis+Long.parseLong(updateTime));
  79. long updateTimes=time.toMillis( true );
  80. Log.d(TAG, "requestnextupdateat" +updateTimes);
  81. AlarmManageralarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
  82. alarm.set(AlarmManager.RTC_WAKEUP,updateTimes,pending);
  83. stopSelf();
  84. }
  85. }

Android实现widget定时更新


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论