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

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

 2011-03-19 11:57:21 来源:本站整理   
核心提示: assert(uri.getPathSegments().isEmpty()); final MatrixCursor c = new MatrixCursor( new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE }); for (int i

     assert(uri.getPathSegments().isEmpty());

   final MatrixCursor c = new MatrixCursor(
         new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE });
     for (int i = 0; i < sData.size(); ++i) {
       final WeatherDataPoint data = sData.get(i);
       c.addRow(new Object[]{ new Integer(i), data.city, new Integer(data.degrees) });
     }
     return c;
   }

   @Override
   public String getType(Uri uri) {
     return "vnd.android.cursor.dir/vnd.weatherlistwidget.citytemperature";
   }

   @Override
   public Uri insert(Uri uri, ContentValues values) {
     return null;
   }

   @Override
   public int delete(Uri uri, String selection, String[] selectionArgs) {
     return 0;
   }

   @Override
   public synchronized int update(Uri uri, ContentValues values, String selection,
       String[] selectionArgs) {
     assert(uri.getPathSegments().size() == 1);

   final int index = Integer.parseInt(uri.getPathSegments().get(0));
     final MatrixCursor c = new MatrixCursor(
         new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE });
     assert(0 <= index && index < sData.size());
     final WeatherDataPoint data = sData.get(index);
     data.degrees = values.getAsInteger(Columns.TEMPERATURE);

   getContext().getContentResolver().notifyChange(uri, null);
     return 1;
   }

}

上面可以看到,对于插入和删除没有做过多的处理,对于天气更新给出了详细的解决方法。

 有关 WeatherWidgetProvider.java 主要是appWidget的核心,为provider

class WeatherDataProviderObserver extends ContentObserver { //监控数据库的变化
   private AppWidgetManager mAppWidgetManager;
   private ComponentName mComponentName;

   WeatherDataProviderObserver(AppWidgetManager mgr, ComponentName cn, Handler h) {
     super(h);
     mAppWidgetManager = mgr;
     mComponentName = cn;
   }

   @Override
   public void onChange(boolean selfChange) {
       mAppWidgetManager.notifyAppWidgetViewDataChanged(
         mAppWidgetManager.getAppWidgetIds(mComponentName), R.id.weather_list);
   }
}

 public class WeatherWidgetProvider extends AppWidgetProvider {
   public static String CLICK_ACTION = "com.example.android.weatherlistwidget.CLICK";
   public static String REFRESH_ACTION = "com.example.android.weatherlistwidget.REFRESH";
   public static String EXTRA_CITY_ID = "com.example.android.weatherlistwidget.city";

   private static HandlerThread sWorkerThread;
   private static Handler sWorkerQueue;
   private static WeatherDataProviderObserver sDataObserver;

   public WeatherWidgetProvider() {
     sWorkerThread = new HandlerThread("WeatherWidgetProvider-worker"); //开一个线程,这里用到了HandlerThread
     sWorkerThread.start();
     sWorkerQueue = new Handler(sWorkerThread.getLooper()); //不了解Thread的Looper可以看下这个例子比较简单清晰

上一页  1 2 3 4 5  下一页

Tags:Android 自带 天气预报

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接