界面组成

  1. 载入界面
  2. 显示界面

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方法跳转到查询到的所有天气的显示界面。下一篇文章讲解。

看下效果图:

<Android 应用 之路> 天气预报(二)的更多相关文章

  1. <Android 应用 之路> 天气预报(五)

    前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...

  2. <Android 应用 之路> 天气预报(四)

    前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...

  3. <Android 应用 之路> 天气预报(一)

    Android天气预报客户端 设计思路 欢迎界面,版本号,应用名 + 数据后台加载(所有城市的信息获取) 数据加载完成后跳转到显示界面,显示所有查询到的城市的天气信息 欢迎界面和天气显示界面分别为单独 ...

  4. <Android 应用 之路> 天气预报(三)

    昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...

  5. redis成长之路——(二)

    redis操作封装 针对这些常用结构,StackExchange.Redis已经做了一些封装,不过在实际应用场景中还必须添加一些功能,例如重试等 所以对一些常功能做了一些自行封装SERedisOper ...

  6. Android学习之路书籍推荐

    Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 JAVA入门书籍: < Introduction to java programming > < Core java & ...

  7. Android系列之网络(二)----HTTP请求头与响应头

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  8. Android系列之Fragment(二)----Fragment的生命周期和返回栈

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  9. Android之自定义生成彩色二维码

    先导个zxing.jar包 下面是xml布局 activity_main.xml <RelativeLayout xmlns:android="http://schemas.andro ...

随机推荐

  1. hadoop2.6.0中自定义分割符

    最近在学习hadoop,用的hadoop2.6.0 然后在学习编写mapreduce程序时,发现默认对文件的输入是采用每行进行分割,下面来分析下改变这个分割方式的办法: 来看看默认是怎样实现的:

  2. [codeforces274b]Zero Tree(树形dp)

    题意:给出一棵树,每个点有权值,每次操作可以对一个联通子集中的点全部加1,或者全部减1,且每次操作必须包含点1,问最少通过多少次操作可以让整棵树每个点的权值变为0. 解题关键:自底向上dp,记录up, ...

  3. Tomcat访问程序外的上传文件

    自己在编写程序时,把图片上传到程序根目录下,但是页面使用<img> 没有显示.但是,当我刷新项目下文件夹后,页面刷新可以显示. 我通过网上查询,当在Tomcat下的server.xml配置 ...

  4. fgets(),fscanf()的输入测试与FILE中的当前指针

    fgets(string,int,fp) 回车读入测试 #include <stdio.h> int main() { FILE *fp; ],ch2[],ch3[],ch4[]; fp= ...

  5. SMB SMB2 协议wiki

    from:https://wiki.wireshark.org/SMB2/BufferCode https://msdn.microsoft.com/en-us/library/ee441872.as ...

  6. java反射机制基础总结

    1反射机制是啥? 反射是运行中的程序检查自己和软件运行环境的能力,它可以根据它发现的进行改变.通俗的讲就是反射可以在运行时根据指定的类名获得类的信息. 2反射机制有啥用? Reflection(反射) ...

  7. P4769 [NOI2018]冒泡排序(dp)

    传送门 日常膜拜shadowice巨巨的题解 //minamoto #include<bits/stdc++.h> #define R register #define ll long l ...

  8. 洛谷P2292 [HNOI2004]L语言

    传送门 建好trie树 当$dp[j]==1$当且仅当存在$dp[k]=1$且$T[k+1,j]==word[i]$ 然后乱搞就行了 //minamoto #include<iostream&g ...

  9. 【并发编程】一文带你读懂深入理解Java内存模型(面试必备)

    并发编程这一块内容,是高级资深工程师必备知识点,25K起如果不懂并发编程,那基本到顶.但是并发编程内容庞杂,如何系统学习?本专题将会系统讲解并发编程的所有知识点,包括但不限于: 线程通信机制,深入JM ...

  10. JAVA之动态编译

    通过Java动态生成class文件 今天说下JAVA中的动态编译,这个功能根据我现在的了解好像没有见到过用的,我Jio的吧,现在的一些在线代码编缉器可以用到了,这个具体我也不是很清楚.感兴趣的大家可以 ...