- public class WidgetService extends Service
- {
- @Override
- public IBinder onBind(Intent intent)
- {
- return null ;
- }
- @Override
- public void onStart(Intent intent, int startId)
- {
- super .onStart(intent, startId);
- RemoteViews rv = new RemoteViews( this .getPackageName(), R.layout.app);
- rv.setTextViewText(R.id.av1, new Date().toLocaleString());
- ComponentName cn = new ComponentName( this , DeskMain. class );
- AppWidgetManager am = AppWidgetManager.getInstance(this );
- am.updateAppWidget(cn, rv);
- }
- }
- public class DeskMain extends AppWidgetProvider
- {
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int [] appWidgetIds)
- {
- Log.i("syso" , "update3..................." );
- Intent intent=new Intent(context ,WidgetService. class );
- PendingIntent refreshIntent=PendingIntent.getService(context, 0 , intent, 0 );
- AlarmManager alarm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
- alarm.setRepeating(AlarmManager.RTC, 0 , 1000 , refreshIntent);
- context.startService(intent);
- }
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Log.i("syso" , "receive..................." );
- super .onReceive(context, intent);
- }
AndroidManifest.xml
- <? xml version = "1.0" encoding = "utf-8" ?>
- < manifest xmlns:android = "http://schemas.android.com/apk/res/android"
- package = "com.app"
- android:versionCode = "1"
- android:versionName = "1.0" >
- < uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" > </ uses-permission >
- < application android:icon = "@drawable/icon" android:label = "@string/app_name" android:screenOrientation = "portrait" android:permission = "android.permission.ACCESS_FINE_LOCATION" >
- < activity android:name = ".Main"
- android:label = "@string/app_name" >
- <!-- -->
- < receiver
- android:label = "@string/app_name"
- android:name = ".DeskMain" >
- < intent-filter >
- < action android:name = "android.appwidget.action.APPWIDGET_UPDATE" />
- </ intent-filter >
- < meta-data android:name = "android.appwidget.provider"
- android:resource = "@xml/my_widget_provider" >
- </ meta-data >
- </ receiver >
- < service android:name = "WidgetService" />
- </ application >
- < uses-sdk android:minSdkVersion = "7" />
- </ manifest >
我们在做widget开发时,要解决的主要问题是怎么做到实时刷新,比如当我们在一个activity里点击插入或删除操作时,能够使widget上显示的数据条数刷新
下面是在activity里要实现的代码:
- Intent intent = new Intent( "com.android.CLICK" );
- ResultActivity.this .sendBroadcast(intent);
我们还要在AndroidMainifest.xml的<intent-filter>里定制:
- <action android:name= "com.android.CLICK" ></action>
在widget类里我们用onReceive方法:
- //把数据显示上widget
- RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
- String strLine = System.getProperty("line.separator" );
- views.setTextViewText(R.id.note_text, “要显示的信息”);
- //添加响应事件
- intent = new Intent(context, OperateActivity. class );
- PendingIntent Pintent= PendingIntent.getActivity(context, 0 , intent, 0 );
- views.setOnClickPendingIntent(R.id.widget, Pintent);
- //最后更新
- AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
- appWidgetManager.updateAppWidget(new ComponentName(context, Note. class ), views);