您的位置:首页 > 移动开发 > Android开发

<Android 应用 之路> 天气预报(二)

2016-05-03 23:15 651 查看

界面组成

载入界面

显示界面

Activity两个,一个用来显示载入界面,一个用来显示天气信息

主要代码如下:

public class MyActivity extends Activity {

private static final String TAG = "WelcomeActiivty";
private ProgressBar mLoadingBar; //载入进度条
private TextView mVersionName;  //载入界面显示版本信息
private AsyncTask mLoadingTask;  //后台载入异步任务
private ArrayList<WeatherInfo> weatherInfoList; //用来保存天气信息

//天气信息接口
private static final String frontUrl = "http://www.weather.com.cn/data/cityinfo/";
private static final String suffix = ".html";
//与上面配合使用,中间添加城市code,如
//http://www.weather.com.cn/data/cityinfo/101010100.html

//所有城市的列表
private int[] mCityCodeList = {101010100, 101010300, 101010400, 101010500, 101010600,
101010700, 101010800, 101010900, 101011000, 101011100, 101011200, 101011300,
101011400, 101011500, 101011600, 101011700, 101011800, 101011900, 101012000,
101012100, 101012200, 101010200, 101030100, 101030300, 101030400, 101030500,
101030600, 101031400, 101030800, 101030900, 101031000, 101031100, 101031200,
101030200, 101030700, 101020100, 101020300, 101020500, 101020600, 101021300,
101020800, 101020900, 101021000, 101021100, 101021200, 101020200, 101020700,
101090101, 101090301, 101090402, 101090501, 101091101, 101090701, 101090801,
101090901, 101091001, 101090201, 101090601, 101180101, 101180301, 101180401,
101180501, 101180601, 101180701, 101180801, 101180901, 101181001, 101181101,
101181201, 101181301, 101181401, 101181501, 101181601, 101181701, 101181801,
101180201, 101220101, 101220301, 101220401, 101220501, 101220601, 101220701,
101220801, 101220901, 101221001, 101221101, 101221201, 101221301, 101221401,
101221501, 101221601, 101221701, 101220201, 101210101, 101211101, 101210201,
101210301, 101210901, 101210501, 101210601, 101210701, 101210801, 101211001,
101210401, 101040100, 101040300, 101040400, 101040500, 101040600, 101040700,
101040800, 101040900, 101041000, 101041100, 101041200, 101041300, 101041400,
101041500, 101041600, 101041700, 101041800, 101041900, 101042000, 101042100,
101042200, 101042300, 101042400, 101042500, 101042600, 101042700, 101042800,
101042900, 101043000, 101043100, 101043200, 101043300, 101043400, 101043600,
101043700, 101040200, 101230101, 101230501, 101230601, 101230701, 101230509,
101230901, 101230201, 101230301, 101230401, 101230801, 101160101, 101160301,
101160401, 101160501, 101160601, 101161401, 101160801, 101160901, 101161001,
101161101, 101161201, 101161301, 101160201, 101160701, 101280101, 101280301,
101280401, 101280501, 101280601, 101280701, 101280800, 101280901, 101281001,
101281101, 101281201, 101281301, 101281401, 101281501, 101281601, 101281701,
101281801, 101281901, 101282001, 101282101, 101280201, 101300101, 101300301,
101300401, 101300501, 101300601, 101301401, 101300801, 101300901, 101301001,
101301101, 101301201, 101301301, 101300201, 101300701, 101260101, 101260301,
101260401, 101260906, 101260601, 101260701, 101260801, 101260201, 101260501,
101290101, 101290301, 101290601, 101290701, 101290801, 101290901, 101291001,
101291101, 101291201, 101291301, 101291401, 101291501, 101291601, 101290201,
101290401, 101290501, 101080101, 101080301, 101080401, 101080501, 101081201,
101080701, 101080801, 101080901, 101081000, 101081101, 101080201, 101080601,
101240101, 101240301, 101240401, 101240501, 101241101, 101240701, 101240801,
101240901, 101241001, 101240201, 101240601, 101200101, 101200501, 101200801,
101200901, 101201001, 101201101, 101201201, 101201301, 101201401, 101201501,
101201601, 101201701, 101200201, 101200301, 101200401, 101200601, 101200701,
101270101, 101270301, 101270401, 101270501, 101270601, 101270701, 101270801,
101270901, 101271001, 101271101, 101271201, 101271301, 101271401, 101271501,
101271601, 101271701, 101271801, 101271901, 101272001, 101272101, 101270201,
101170101, 101170501, 101170401, 101170201, 101170301, 101150101, 101150301,
101150801, 101150501, 101150601, 101150701, 101150201, 101150401, 101120101,
101120601, 101120901, 101121001, 101121101, 101121201, 101121301, 101121401,
101121501, 101121601, 101121701, 101120201, 101120301, 101120401, 101120501,
101120701, 101120801, 101110101, 101110300, 101110401, 101111001, 101110601,
101110701, 101110801, 101110901, 101110200, 101110501, 101100101, 101100701,
101100801, 101100901, 101101001, 101100501, 101100201, 101100301, 101100401,
101100601, 101101100, 101130101, 101130301, 101130401, 101130501, 101130601,
101130701, 101130801, 101130901, 101131001, 101131101, 101131201, 101131301,
101131401, 101131501, 101131601, 101130201, 101140101, 101140301, 101140701,
101140501, 101140601, 101140201, 101140401, 101340101, 101340201, 101340401,
101310101, 101310201, 101310202, 101310203, 101310204, 101310205, 101310206,
101310207, 101310208, 101310209, 101310210, 101310211, 101310212, 101310214,
101310215, 101310216, 101310217, 101310220, 101310221, 101310222, 101310102,
101250101, 101250301, 101250401, 101250501, 101250601, 101250700, 101250801,
101250901, 101251001, 101251101, 101251201, 101251301, 101251401, 101251501,
101250201, 101190101, 101190301, 101190401, 101190501, 101190601, 101191301,
101190801, 101190901, 101191001, 101191101, 101191201, 101190201, 101190701,
101050101, 101050301, 101050401, 101050501, 101050601, 101051301, 101050801,
101050901, 101051002, 101051101, 101051201, 101050201, 101050701, 101060101,
101060301, 101060401, 101060901, 101060601, 101060701, 101060801, 101060201,
101060501, 101070101, 101070301, 101070401, 101070501, 101070601, 101071401,
101070801, 101070901, 101071001, 101071101, 101071201, 101071301, 101070201,
101070701};

private int mAvailableLinks = mCityCodeList.length;//用来保存可用的查询结果,后面用来判断载入是否完成

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题的界面效果
setContentView(R.layout.welcome);

weatherInfoList = new ArrayList<WeatherInfo>();

mLoadingBar = (ProgressBar) findViewById(R.id.loadingbar);
mLoadingBar.setProgress(0);
mLoadingBar.setMax(mCityCodeList.length);//进度条

mVersionName = (TextView) findViewById(R.id.versionname);

mVersionName.setText(getString(R.string.app_name).toString() + "  " + getLocalVersionName(this));

final RequestQueue mRequestQueue = Volley.newRequestQueue(this);
AsyncTask mLoadingDataTask = new AsyncTask() {

@Override
protected void onProgressUpdate(Object[] values) {
super.onProgressUpdate(values);
int progress = mLoadingBar.getProgress();
mLoadingBar.setProgress(progress + 1);//更新进度条
if (mLoadingBar.getProgress() == mAvailableLinks) {
showWeatherInfo();
}
}

@Override
protected Object doInBackground(Object[] objects) {
//查询所有的信息并解析出来保存到ArrayList中
for (int i = 0; i < mCityCodeList.length; i++) {
String uri = frontUrl + String.valueOf(mCityCodeList[i]) + suffix;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(uri, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
JSONObject weatherinfojson = jsonObject.getJSONObject("weatherinfo");
WeatherInfo newWeatherInfo = new WeatherInfo(
weatherinfojson.getString("city"),
weatherinfojson.getString("cityid"),
weatherinfojson.getString("temp1"),
weatherinfojson.getString("temp2"),
weatherinfojson.getString("weather"),
weatherinfojson.getString("img1"),
weatherinfojson.getString("img2"),
weatherinfojson.getString("ptime"));
publishProgress();
weatherInfoList.add(newWeatherInfo);
} catch (Exception e) {
mAvailableLinks--;
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
mAvailableLinks--;
Log.e("TAG", volleyError.getMessage(), volleyError);
}
});
//添加到RequestQueue中
mRequestQueue.add(jsonObjectRequest);
}
return null;
}
};
mLoadingDataTask.execute();

}

/**
* 获取本地软件版本名称
*/
public static String getLocalVersionName(Context ctx) {
String localVersion = "";
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
//跳转到显示天气信息的界面,并传递List数据
private void showWeatherInfo() {
Bundle bundle = new Bundle();
bundle.putSerializable(Utils.WEATHERINFO, (Serializable) weatherInfoList);
Intent intent = new Intent(MyActivity.this, MainActivity.class);
intent.putExtra(Utils.WEATHERINFO, bundle);
startActivity(intent);
finish();//finish掉这个Activity,否则返回键可以回到载入界面 那就尴尬了!
}
}


Activity之前传递数据的方式 Bundle

public void putAll(Bundle bundle)

public boolean hasFileDescriptors()

public void putByte(String key, byte value)

public void putChar(String key, char value)

public void putShort(String key, short value)

public void putFloat(String key, float value)

public void putCharSequence(String key, CharSequence value)

public void putParcelable(String key, Parcelable value)

public void putSize(String key, Size value)

public void putSizeF(String key, SizeF value)

public void putParcelableArray(String key, Parcelable[] value)

public void putParcelableArrayList(String key, ArrayList< xx extends Parcelable> value)

public void putSparseParcelableArray(String key, SparseArray< xx extends Parcelable> value)

public void putIntegerArrayList(String key, ArrayList value)

public void putStringArrayList(String key, ArrayList value)

public void putCharSequenceArrayList(String key, ArrayList value)

public void putSerializable(String key, Serializable value)

public void putByteArray(String key, byte[] value)

public void putShortArray(String key, short[] value)

public void putCharArray(String key, char[] value)

public void putFloatArray(String key, float[] value)

public void putCharSequenceArray(String key, CharSequence[] value)

public void putBundle(String key, Bundle value)

public void putBinder(String key, IBinder value)

很多类似的方法,Bundle就是一个载体,用什么方式封装后传递,在对应端就用什么方式把它解读出来

通过showWeatherInfo方法跳转到查询到的所有天气的显示界面。下一篇文章讲解。

看下效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: