Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android平台將內置天氣預報程序

Android平台將內置天氣預報程序

編輯:高級開發

 從android的Git項目控制管理上來看多出的DeskClock.git包中包含了DeskClock.Java中我們可以看到有關天氣的內容,比如

mWeatherCurrentTemperature = (TextView) findVIEwById(R.id.weather_temperature);
mWeatherHighTemperature = (TextView) findVIEwById(R.id.weather_high_temperature);
mWeatherLowTemperature = (TextView) findVIEwById(R.id.weather_low_temperature);
mWeatherLocation = (TextView) findVIEwById(R.id.weather_location);
mWeatherIcon = (ImageView) findVIEwById(R.id.weather_icon);

當然目前該版本尚未完整,其中有關天氣更新從獨立的服務中獲取天氣數據保存到content provider數據庫中,通過下面的代碼讀取:

private void queryWeatherData() {
// if we couldn't load the weather widget's resources, we simply
// assume it's not present on the device.
if (mGenIEResources == null) return;

Uri queryUri = new Uri.Builder()
.scheme(android.content.ContentResolver.SCHEME_CONTENT)
.authority(WEATHER_CONTENT_AUTHORITY)
.path(WEATHER_CONTENT_PATH)
.appendPath(new Long(System.currentTimeMillis()).toString())
.build();

if (DEBUG) Log.d(LOG_TAG, "querying genIE: " + queryUri);

Cursor cur;
try {
cur = managedQuery(
queryUri,
WEATHER_CONTENT_COLUMNS,
null,
null,
null);
} catch (RuntimeException e) {
Log.e(LOG_TAG, "Weather query failed", e);
cur = null;
}

if (cur != null && cur.moveToFirst()) {
if (DEBUG) {
Java.lang.StringBuilder sb =
new Java.lang.StringBuilder("Weather query result: {");
for(int i=0; i<cur.getColumnCount(); i++) {
if (i>0) sb.append(", ");
sb.append(cur.getColumnName(i))
.append("=")
.append(cur.getString(i));
}
sb.append("}");
Log.d(LOG_TAG, sb.toString());
}

mWeatherIconDrawable = mGenIEResources.getDrawable(cur.getInt(
cur.getColumnIndexOrThrow("iconResId")));

mWeatherLocationString = cur.getString(
cur.getColumnIndexOrThrow("location"));

// any of these may be NULL
final int colTemp = cur.getColumnIndexOrThrow("temperature");
final int colHigh = cur.getColumnIndexOrThrow("highTemperature");
final int colLow = cur.getColumnIndexOrThrow("lowTemperature");

mWeatherCurrentTemperatureString =
cur.isNull(colTemp)
? "\u2014"
: String.format("%d\u00b0", cur.getInt(colTemp));
mWeatherHighTemperatureString =
cur.isNull(colHigh)
? "\u2014"
: String.format("%d\u00b0", cur.getInt(colHigh));
mWeatherLowTemperatureString =
cur.isNull(colLow)
? "\u2014"
: String.format("%d\u00b0", cur.getInt(colLow));
} else {
Log.w(LOG_TAG, "No weather information available (cur="
+ cur +")");
mWeatherIconDrawable = null;
mWeatherLocationString = getString(R.string.weather_fetch_failure);
mWeatherCurrentTemperatureString =
mWeatherHighTemperatureString =
mWeatherLowTemperatureString = "";
}

mHandy.sendEmptyMessage(UPDATE_WEATHER_DISPLAY_MSG);
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved