package com.dybweather.app.service;

import com.dybweather.app.util.HttpCallbackListener;
import com.dybweather.app.util.HttpUtil;
import com.dybweather.app.util.Utility;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;

public class AutoUpdateService extends Service {
	
	public static final String TAG = "AutoUpdateService";

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				updateWeather();
			}
		}).start();
		AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
		int anHour = 60 * 1000; //60
		long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
		Intent i = new Intent(this, AutoUpdateService.class);
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
		manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
		return super.onStartCommand(intent, flags, startId);
	}
	
	private void updateWeather(){
		Log.d(TAG, "updateWeather==================>");
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		String weatherCode = prefs.getString("weather_code", "");
		String address = "http://www.weather.com.cn/data/cityinfo/" + weatherCode + ".html";
		HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
			
			@Override
			public void onFinish(String response) {
				Utility.handleWeatherResponse(AutoUpdateService.this, response);
			}
			
			@Override
			public void onError(Exception e) {
				e.printStackTrace();
			}
		});
	}

}

@@ -41,133 +41,22 @@
 ort 
-com.dybweather.app.util.HttpCallbackListener;%0Aimport com.dybweather.app.util.HttpUtil;%0Aimport com.dybweather.app.util.Utility
+java.util.Date
 ;%0A%0Ai
@@ -351,16 +351,206 @@
 l.Log;%0A%0A
+import com.dybweather.app.receiver.AutoUpdateReceiver;%0Aimport com.dybweather.app.util.HttpCallbackListener;%0Aimport com.dybweather.app.util.HttpUtil;%0Aimport com.dybweather.app.util.Utility;%0A%0A
 public c
@@ -865,24 +865,80 @@
 oid run() %7B%0A
+%09%09%09%09Log.d(TAG, %22executed at %22 + new Date().toString());%0A
 %09%09%09%09updateWe
@@ -1057,16 +1057,21 @@
 r = 60 *
+ 60 *
  1000; /
@@ -1071,18 +1071,19 @@
 1000; //
-60
+1%D0%A1%CA%B1
 %0A%09%09long 
@@ -1178,23 +1178,24 @@
 toUpdate
-Service
+Receiver
 .class);
@@ -1426,58 +1426,8 @@
 ()%7B%0A
-%09%09Log.d(TAG, %22updateWeather==================%3E%22);%0A
 %09%09Sh
@@ -1503,16 +1503,16 @@
 (this);%0A
-
 %09%09String
@@ -1563,16 +1563,93 @@
 %22, %22%22);%0A
+%09%09Log.d(TAG, %22updateWeather==================%3EweatherCode: %22 + weatherCode);%0A
 %09%09String

修改自动更新天气的bug。