android widget的自动更新

service类
  1. public   class  WidgetService  extends  Service  
  2. {  
  3.   
  4.     @Override   
  5.     public  IBinder onBind(Intent intent)  
  6.     {         
  7.         return   null ;  
  8.     }  
  9.       
  10.     @Override   
  11.     public   void  onStart(Intent intent,  int  startId)  
  12.     {     
  13.         super .onStart(intent, startId);  
  14.         RemoteViews rv = new  RemoteViews( this .getPackageName(), R.layout.app);  
  15.         rv.setTextViewText(R.id.av1, new  Date().toLocaleString());  
  16.         ComponentName cn = new  ComponentName( this , DeskMain. class );  
  17.         AppWidgetManager am = AppWidgetManager.getInstance(this );  
  18.         am.updateAppWidget(cn, rv);  
  19.     }  
  20. }  
AppWidgetProvider类
  1. public   class  DeskMain  extends  AppWidgetProvider  
  2. {  
  3.   
  4.     @Override   
  5.     public   void  onUpdate(Context context, AppWidgetManager appWidgetManager,  
  6.             int [] appWidgetIds)  
  7.     {  
  8.         Log.i("syso" "update3..................." );  
  9.         Intent intent=new  Intent(context ,WidgetService. class );  
  10.         PendingIntent refreshIntent=PendingIntent.getService(context, 0 , intent,  0 );  
  11.         AlarmManager alarm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);  
  12.         alarm.setRepeating(AlarmManager.RTC, 0 1000 , refreshIntent);  
  13.         context.startService(intent);        
  14.     }  
  15.       
  16.     @Override   
  17.     public   void  onReceive(Context context, Intent intent)  
  18.     {  
  19.         Log.i("syso" "receive..................." );  
  20.         super .onReceive(context, intent);  
  21.     } 
AndroidManifest.xml
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < manifest   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.       package = "com.app"   
  4.       android:versionCode = "1"   
  5.       android:versionName = "1.0" >   
  6. < uses-permission   android:name = "android.permission.ACCESS_FINE_LOCATION" > </ uses-permission >   
  7.   
  8.   
  9.     < application   android:icon = "@drawable/icon"   android:label = "@string/app_name"   android:screenOrientation = "portrait"   android:permission = "android.permission.ACCESS_FINE_LOCATION" >   
  10.         < activity   android:name = ".Main"   
  11.                   android:label = "@string/app_name" >   
  12.      <!--  -->   
  13.     < receiver   
  14.       android:label = "@string/app_name"   
  15.       android:name = ".DeskMain" >   
  16.             < intent-filter >     
  17.                 < action   android:name = "android.appwidget.action.APPWIDGET_UPDATE"   />     
  18.             </ intent-filter >     
  19.      < meta-data   android:name = "android.appwidget.provider"   
  20.         android:resource = "@xml/my_widget_provider"   >   
  21.       </ meta-data >   
  22.     </ receiver >   
  23.     < service   android:name = "WidgetService"   />   
  24.       
  25.     </ application >   
  26.     < uses-sdk   android:minSdkVersion = "7"   />     
  27.       
  28. </ manifest >  



我们在做widget开发时,要解决的主要问题是怎么做到实时刷新,比如当我们在一个activity里点击插入或删除操作时,能够使widget上显示的数据条数刷新
下面是在activity里要实现的代码:
  1. Intent intent =  new  Intent( "com.android.CLICK" );  
  2.             ResultActivity.this .sendBroadcast(intent); 

我们还要在AndroidMainifest.xml的<intent-filter>里定制:
  1. <action android:name= "com.android.CLICK" ></action> 

在widget类里我们用onReceive方法:
  1. //把数据显示上widget   
  2.             RemoteViews views = new  RemoteViews(context.getPackageName(), R.layout.main);  
  3.             String strLine = System.getProperty("line.separator" );  
  4.             views.setTextViewText(R.id.note_text, “要显示的信息”);  
  5.               
  6.             //添加响应事件   
  7.             intent = new  Intent(context, OperateActivity. class );    
  8.               
  9.               
  10.             PendingIntent Pintent= PendingIntent.getActivity(context, 0 , intent,  0 );  
  11.             views.setOnClickPendingIntent(R.id.widget, Pintent);  
  12.               
  13.             //最后更新   
  14.             AppWidgetManager  appWidgetManager=AppWidgetManager.getInstance(context);  
  15.             appWidgetManager.updateAppWidget(new  ComponentName(context, Note. class ), views); 

你可能感兴趣的:(android,service,encoding)