Android Open Source - MyWeather Auto Update Service






From Project

Back to project page MyWeather.

License

The source code is released under:

Apache License

If you think the Android project MyWeather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package service;
/*from  ww  w  . j  ava 2  s .  c  o m*/
import util.HttpCallbackListener;
import util.HttpUtil;
import 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;

public class AutoUpdateService extends Service {

  @Override
  public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    new Thread(new Runnable() {
      
      @Override
      public void run() {
        // TODO Auto-generated method stub
        updateWeatherInfo();
      }
    }).start();
    AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
    int anHour = 8 * 60 * 60 * 1000;
    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);
  }
  
  /**
   * update weather info
   */
  private void updateWeatherInfo(){
    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) {
        // TODO Auto-generated method stub
        Utility.handleWeatherResponse(AutoUpdateService.this, response);
      }
      
      @Override
      public void onError(Exception e) {
        // TODO Auto-generated method stub
        e.printStackTrace();
      }
    });
  }

}




Java Source Code List

activity.ChooseAreaActivity.java
activity.WeatherActivity.java
db.MyWeatherDB.java
db.MyWeatherOpenHelper.java
model.City.java
model.County.java
model.Province.java
receiver.AutoUpdateReceiver.java
service.AutoUpdateService.java
util.HttpCallbackListener.java
util.HttpUtil.java
util.Utility.java