Android Open Source - firstcodeandroid Weather Activity






From Project

Back to project page firstcodeandroid.

License

The source code is released under:

MIT License

If you think the Android project firstcodeandroid 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 com.vjia.coolweather.activity;
/*  w  w w.jav  a2s  . c  o  m*/
import com.vjia.coolweather.R;
import com.vjia.coolweather.util.HttpCallbackListener;
import com.vjia.coolweather.util.HttpUtil;
import com.vjia.coolweather.util.Utility;

//import android.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherActivity extends Activity implements OnClickListener{

  private LinearLayout weatherInfoLayout;
  private TextView cityNameText;
  private TextView publishText;
  private TextView weatherDespText;
  private TextView temp1Text;
  private TextView temp2Text;
  private TextView currentDateText;

  // 2 buttons used for phase 3 APK
  private Button switchButton;
  private Button refreshButton;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.weather_layout);
    // initilize
    weatherInfoLayout = (LinearLayout) findViewById(R.id.weather_info_layout);
    cityNameText = (TextView) findViewById(R.id.publish_text);
    publishText = (TextView) findViewById(R.id.publish_text);
    weatherDespText = (TextView) findViewById(R.id.weather_desp);
    temp1Text = (TextView) findViewById(R.id.temp1);
    temp2Text = (TextView) findViewById(R.id.temp2);
    currentDateText = (TextView) findViewById(R.id.current_date);

    String countyCode = getIntent().getStringExtra("county_code");
    if (!TextUtils.isEmpty(countyCode)) {
      // query weather only county
      publishText.setText("Querying...");
      weatherInfoLayout.setVisibility(View.INVISIBLE);
      cityNameText.setVisibility(View.INVISIBLE);
      queryWeatherCode(countyCode);
    } else {
      // show local weather if no county selected
      showWeather();
    }
    
    // logic for phase 3 
    switchButton = (Button)this.findViewById(R.id.switch_city);
    refreshButton = (Button)this.findViewById(R.id.refresh_weather);
    switchButton.setOnClickListener(this);
    refreshButton.setOnClickListener(this);
  }

  private void queryWeatherCode(String countyCode) {
    // TODO Auto-generated method stub
    String address = "http://www.weather.com.cn/data/list3/city"
        + countyCode + ".xml";
    queryFromServer(address, "countyCode");
  }

  private void queryWeatherInfo(String weatherCode) {
    String address = "http://www.weather.com.cn/data/cityinfo/"
        + weatherCode + ".html";
    queryFromServer(address, "weatherCode");

  }

  /**
   * ??????????????????????????????????????????????
   * 
   * @param address
   * @param string
   */
  private void queryFromServer(String address, final String type) {
    // TODO Auto-generated method stub
    HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {

      @Override
      public void onFinish(String response) {
        // TODO Auto-generated method stub
        if ("countyCode".equals(type)) {
          if (!TextUtils.isEmpty(response)) {
            // ???????????????????????????????
            String[] array = response.split("\\|");
            if (array != null && array.length == 2) {
              String weatherCode = array[1];
              queryWeatherInfo(weatherCode);
            }
          }
        } else if ("weatherCode".equals(type)) {
          // ?????????????????????
          Utility.handleWeatherResponse(WeatherActivity.this,
              response);
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              // TODO Auto-generated method stub
              showWeather();
            }
          });
        }
      }

      @Override
      public void onError(Exception e) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {

          @Override
          public void run() {
            // TODO Auto-generated method stub
            publishText.setText("Fail to sync the weather.");
          }

        });
      }

    });
  }

  /**
   * ??SharedPreferences??????????????????????????????????
   */
  private void showWeather() {
    // TODO Auto-generated method stub
    SharedPreferences prefs = PreferenceManager
        .getDefaultSharedPreferences(this);
    cityNameText.setText(prefs.getString("city_name", ""));
    temp1Text.setText(prefs.getString("temp1", ""));
    temp2Text.setText(prefs.getString("temp2", ""));
    weatherDespText.setText(prefs.getString("weather_desp", ""));
    publishText.setText(prefs.getString("publish_time", "")+" renew");
    currentDateText.setText(prefs.getString("current_date", ""));
    weatherInfoLayout.setVisibility(View.VISIBLE);
    cityNameText.setVisibility(View.VISIBLE);
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.switch_city:
      Intent intent = new Intent(this, ChooseAreaActivity.class);
      intent.putExtra("from_weather_activity", true);
      startActivity(intent);
      finish();
      break;
    case R.id.refresh_weather:
      publishText.setText("Syncing...");
      SharedPreferences prefs  = PreferenceManager.getDefaultSharedPreferences(this);
      String weatherCode = prefs.getString("weather_code", "");
      if(!TextUtils.isEmpty(weatherCode)){
        queryWeatherInfo(weatherCode);
      }
      break;
    default:
      break;
    }
  }

}




Java Source Code List

com.example.activitylifecycletest.DialogActivity.java
com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.NormalActivity.java
com.example.activitytest.FirstActivity.java
com.example.listviewtest.FruitAdapter.java
com.example.listviewtest.Fruit.java
com.example.listviewtest.MainActivity.java
com.jikexueyuan.counttime.MainActivity.java
com.jikexueyuan.getmyphonenumber.GetNumber.java
com.jikexueyuan.getmyphonenumber.MainActivity.java
com.jikexueyuan.getmyphonenumber.MyAdapter.java
com.jikexueyuan.getmyphonenumber.PhoneInfo.java
com.vjia.bookcollector.MainActivity.java
com.vjia.coolweather.MainActivity.java
com.vjia.coolweather.activity.ChooseAreaActivity.java
com.vjia.coolweather.activity.WeatherActivity.java
com.vjia.coolweather.db.CoolWeatherDB.java
com.vjia.coolweather.db.CoolWeatherOpenHelper.java
com.vjia.coolweather.model.City.java
com.vjia.coolweather.model.County.java
com.vjia.coolweather.model.Province.java
com.vjia.coolweather.util.HttpCallbackListener.java
com.vjia.coolweather.util.HttpUtil.java
com.vjia.coolweather.util.Utility.java
com.vjia.helloandroid.FirstActivity.java
com.vjia.helloandroid.HelloAndroidActivity.java
com.vjia.hellonote.AddContent.java
com.vjia.hellonote.MainActivity.java
com.vjia.hellonote.MyAdapter.java
com.vjia.hellonote.NotesDB.java
com.vjia.hellonote.SelectAct.java
com.vjia.jokeking.GetJoke.java
com.vjia.jokeking.HttpCallbackListener.java
com.vjia.jokeking.HttpUtil.java
com.vjia.jokeking.Joke.java
com.vjia.jokeking.MainActivity.java
com.vjia.jokeking.MyAdapter.java
com.vjia.locationtest.MainActivity.java