- public class NewsWidgetProvider extends AppWidgetProvider{
- @Override
- public void onDeleted(Contextcontext, int []appWidgetIds){
- }
- @Override
- public void onDisabled(Contextcontext){
- //TODOAuto-generatedmethodstub
- super .onDisabled(context);
- }
- @Override
- public void onEnabled(Contextcontext){
- //TODOAuto-generatedmethodstub
- super .onEnabled(context);
- }
- @Override
- public void onUpdate(Contextcontext,AppWidgetManagerappWidgetManager, int []appWidgetIds){
- NewsService.updateAppWidgetIds(appWidgetIds);
- context.startService( new Intent(context,NewsService. class ));
- }
- public static RemoteViewsupdateAppWidget(Contextcontext,List<RssNews>list){
- RemoteViewsviews= 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);
- }
- IntentdetailIntent= new Intent(context,NewsSiteList. class );
- PendingIntentpending=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(Contextcontext,Intentintent){
- super .onReceive(context,intent);
- }
- }
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:
- public class NewsService extends Service implements Runnable{
- private static final StringTAG= "NewsService" ;
- private static Queue<Integer>sAppWidgetIds= new LinkedList<Integer>();
- public static final StringACTION_UPDATE_ALL= "com.xxxx.news.UPDATE_ALL" ;
- private static boolean sThreadRunning= false ;
- private static ObjectsLock= new Object();
- @Override
- public IBinderonBind(Intentintent){
- return null ;
- }
- public static void updateAppWidgetIds( int []appWidgetIds){
- synchronized (sLock){
- for ( int appWidgetId:appWidgetIds){
- sAppWidgetIds.add(appWidgetId);
- }
- }
- }
- public static int getNextWidgetId(){
- synchronized (sLock){
- if (sAppWidgetIds.peek()== null ){
- return AppWidgetManager.INVALID_APPWIDGET_ID;
- } else {
- return sAppWidgetIds.poll();
- }
- }
- }
- private static boolean hasMoreUpdates(){
- synchronized (sLock){
- boolean hasMore=!sAppWidgetIds.isEmpty();
- if (!hasMore){
- sThreadRunning= false ;
- }
- return hasMore;
- }
- }
- @Override
- public void onCreate(){
- super .onCreate();
- }
- @Override
- public void onStart(Intentintent, int startId){
- super .onStart(intent,startId);
- if ( null !=intent){
- if (ACTION_UPDATE_ALL.equals(intent.getAction())){
- AppWidgetManagerwidget=AppWidgetManager.getInstance( this );
- updateAppWidgetIds(widget.getAppWidgetIds( new ComponentName( this ,NewsWidgetProvider. class )));
- }
- }
- synchronized (sLock){
- if (!sThreadRunning){
- sThreadRunning= true ;
- new Thread( this ).start();
- }
- }
- }
- @Override
- public void run(){
- SharedPreferencessetting=getSharedPreferences( "com.xxxx.news_preferences" , 0 );
- StringupdateTime=setting.getString( "list_time" , "1800000" );
- StringupdateUrl=setting.getString( "list_site" , "xxxxxxxxx" );
- AppWidgetManagerappWidgetManager=AppWidgetManager.getInstance( this );
- RemoteViewsupdateViews= null ;
- while (hasMoreUpdates()){
- int appWidgetId=getNextWidgetId();
- List<RssNews>listNews=NewsContenttList.getNewsList(updateUrl);
- if (listNews!= null ){
- updateViews=NewsWidgetProvider.updateAppWidget( this ,listNews);
- }
- if (updateViews!= null ){
- appWidgetManager.updateAppWidget(appWidgetId,updateViews);
- }
- }
- IntentupdateIntent= new Intent(ACTION_UPDATE_ALL);
- updateIntent.setClass( this ,NewsService. class );
- PendingIntentpending=PendingIntent.getService( this , 0 ,updateIntent, 0 );
- Timetime= new Time();
- long nowMillis=System.currentTimeMillis();
- time.set(nowMillis+Long.parseLong(updateTime));
- long updateTimes=time.toMillis( true );
- Log.d(TAG, "requestnextupdateat" +updateTimes);
- AlarmManageralarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
- alarm.set(AlarmManager.RTC_WAKEUP,updateTimes,pending);
- stopSelf();
- }
- }