WEB开发网
开发学院手机开发Android 开发 Android 3.0自带的天气预报例子代码 阅读

Android 3.0自带的天气预报例子代码

 2011-03-19 11:57:21 来源:本站整理   
核心提示: } }); } else if (action.equals(CLICK_ACTION)) { final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_AP

         }
       });
     } else if (action.equals(CLICK_ACTION)) {
       final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
           AppWidgetManager.INVALID_APPWIDGET_ID);
       final String city = intent.getStringExtra(EXTRA_CITY_ID);
       final String formatStr = ctx.getResources().getString(R.string.toast_format_string);
       Toast.makeText(ctx, String.format(formatStr, city), Toast.LENGTH_SHORT).show();
     }

   super.onReceive(ctx, intent);
   }

   @Override
   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          for (int i = 0; i < appWidgetIds.length; ++i) {
             final Intent intent = new Intent(context, WeatherWidgetService.class);  //当桌面上有多个这个相同的appWidget需要分别处理
       intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
       intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
       final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
       rv.setRemoteAdapter(appWidgetIds[i], R.id.weather_list, intent);

     rv.setEmptyView(R.id.weather_list, R.id.empty_view);

     final Intent onClickIntent = new Intent(context, WeatherWidgetProvider.class);
       onClickIntent.setAction(WeatherWidgetProvider.CLICK_ACTION);
       onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
       onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
       final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0,
           onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
       rv.setPendingIntentTemplate(R.id.weather_list, onClickPendingIntent);

     final Intent refreshIntent = new Intent(context, WeatherWidgetProvider.class);
       refreshIntent.setAction(WeatherWidgetProvider.REFRESH_ACTION);
       final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0,
           refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
       rv.setOnClickPendingIntent(R.id.refresh, refreshPendingIntent);

     appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
     }
     super.onUpdate(context, appWidgetManager, appWidgetIds);
   }
}

对于WeatherWidgetService.java这个Service集成于RemoteViewsService,主要是UI上的处理

public class WeatherWidgetService extends RemoteViewsService {
   @Override
   public RemoteViewsFactory onGetViewFactory(Intent intent) {
     return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
   }
}

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

上一页  1 2 3 4 5  下一页

Tags:Android 自带 天气预报

编辑录入:coldstar [复制链接] [打 印]
赞助商链接