3 | # Encoding is UTF-8
4 |
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/bin/res/crunch/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/bin/res/crunch/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/bin/res/crunch/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/bin/res/crunch/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/resources.ap_:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/bin/resources.ap_
--------------------------------------------------------------------------------
/gen/com/melhc/simpleweather/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.melhc.simpleweather;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/com/melhc/simpleweather/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.melhc.simpleweather;
9 |
10 | public final class R {
11 | public static final class attr {
12 | /** Must be a reference to another resource, in the form "@[+][package :]type :name
"
13 | or to a theme attribute in the form "?[package :][type :]name
".
14 | */
15 | public static final int leftBackground=0x7f010003;
16 | /**
Must be a reference to another resource, in the form "@[+][package :]type :name
"
17 | or to a theme attribute in the form "?[package :][type :]name
".
18 | */
19 | public static final int rightBackground=0x7f010004;
20 | /**
Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
21 |
This may also be a reference to a resource (in the form
22 | "@[package :]type :name
") or
23 | theme attribute (in the form
24 | "?[package :][type :]name
")
25 | containing a value of this type.
26 | */
27 | public static final int title=0x7f010000;
28 | /**
Must be a color value, in the form of "#rgb
", "#argb
",
29 | "#rrggbb
", or "#aarrggbb
".
30 |
This may also be a reference to a resource (in the form
31 | "@[package :]type :name
") or
32 | theme attribute (in the form
33 | "?[package :][type :]name
")
34 | containing a value of this type.
35 | */
36 | public static final int titleTextColor=0x7f010002;
37 | /**
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp
".
38 | Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),
39 | in (inches), mm (millimeters).
40 |
This may also be a reference to a resource (in the form
41 | "@[package :]type :name
") or
42 | theme attribute (in the form
43 | "?[package :][type :]name
")
44 | containing a value of this type.
45 | */
46 | public static final int titleTextSize=0x7f010001;
47 | }
48 | public static final class dimen {
49 | /** Default screen margins, per the Android Design guidelines.
50 |
51 | Customize dimensions originally defined in res/values/dimens.xml (such as
52 | screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
53 |
54 | */
55 | public static final int activity_horizontal_margin=0x7f040000;
56 | public static final int activity_vertical_margin=0x7f040001;
57 | }
58 | public static final class drawable {
59 | public static final int home=0x7f020000;
60 | public static final int ic_launcher=0x7f020001;
61 | public static final int logo=0x7f020002;
62 | public static final int refresh=0x7f020003;
63 | }
64 | public static final class id {
65 | public static final int action_settings=0x7f08000c;
66 | public static final int city_name=0x7f080004;
67 | public static final int current_date=0x7f080008;
68 | public static final int list_view=0x7f080001;
69 | public static final int publish_text=0x7f080006;
70 | public static final int refresh_weather=0x7f080005;
71 | public static final int switch_city=0x7f080003;
72 | public static final int temp1=0x7f08000a;
73 | public static final int temp2=0x7f08000b;
74 | public static final int textView1=0x7f080002;
75 | public static final int title_text=0x7f080000;
76 | public static final int weather_desp=0x7f080009;
77 | public static final int weather_info_layout=0x7f080007;
78 | }
79 | public static final class layout {
80 | public static final int choose_area=0x7f030000;
81 | public static final int choose_area_item=0x7f030001;
82 | public static final int weather_layout=0x7f030002;
83 | }
84 | public static final class menu {
85 | public static final int main=0x7f070000;
86 | }
87 | public static final class string {
88 | public static final int a=0x7f050003;
89 | public static final int action_settings=0x7f050001;
90 | public static final int app_name=0x7f050000;
91 | public static final int hello_world=0x7f050002;
92 | }
93 | public static final class style {
94 | /**
95 | Base application theme, dependent on API level. This theme is replaced
96 | by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
97 |
98 |
99 | Theme customizations available in newer API levels can go in
100 | res/values-vXX/styles.xml, while customizations related to
101 | backward-compatibility can go here.
102 |
103 |
104 | Base application theme for API 11+. This theme completely replaces
105 | AppBaseTheme from res/values/styles.xml on API 11+ devices.
106 |
107 | API 11 theme customizations can go here.
108 |
109 | Base application theme for API 14+. This theme completely replaces
110 | AppBaseTheme from BOTH res/values/styles.xml and
111 | res/values-v11/styles.xml on API 14+ devices.
112 |
113 | API 14 theme customizations can go here.
114 | */
115 | public static final int AppBaseTheme=0x7f060000;
116 | /** Application theme.
117 | All customizations that are NOT specific to a particular API-level can go here.
118 | */
119 | public static final int AppTheme=0x7f060001;
120 | }
121 | public static final class styleable {
122 | /** Attributes that can be used with a Topball.
123 |
Includes the following attributes:
124 |
125 |
126 |
127 | Attribute Description
128 | {@link #Topball_leftBackground com.melhc.simpleweather:leftBackground}
129 | {@link #Topball_rightBackground com.melhc.simpleweather:rightBackground}
130 | {@link #Topball_title com.melhc.simpleweather:title}
131 | {@link #Topball_titleTextColor com.melhc.simpleweather:titleTextColor}
132 | {@link #Topball_titleTextSize com.melhc.simpleweather:titleTextSize}
133 |
134 | @see #Topball_leftBackground
135 | @see #Topball_rightBackground
136 | @see #Topball_title
137 | @see #Topball_titleTextColor
138 | @see #Topball_titleTextSize
139 | */
140 | public static final int[] Topball = {
141 | 0x7f010000, 0x7f010001, 0x7f010002, 0x7f010003,
142 | 0x7f010004
143 | };
144 | /**
145 | This symbol is the offset where the {@link com.melhc.simpleweather.R.attr#leftBackground}
146 | attribute's value can be found in the {@link #Topball} array.
147 |
148 |
149 |
Must be a reference to another resource, in the form "@[+][package :]type :name
"
150 | or to a theme attribute in the form "?[package :][type :]name
".
151 | @attr name com.melhc.simpleweather:leftBackground
152 | */
153 | public static final int Topball_leftBackground = 3;
154 | /**
155 |
This symbol is the offset where the {@link com.melhc.simpleweather.R.attr#rightBackground}
156 | attribute's value can be found in the {@link #Topball} array.
157 |
158 |
159 |
Must be a reference to another resource, in the form "@[+][package :]type :name
"
160 | or to a theme attribute in the form "?[package :][type :]name
".
161 | @attr name com.melhc.simpleweather:rightBackground
162 | */
163 | public static final int Topball_rightBackground = 4;
164 | /**
165 |
This symbol is the offset where the {@link com.melhc.simpleweather.R.attr#title}
166 | attribute's value can be found in the {@link #Topball} array.
167 |
168 |
169 |
Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
170 |
This may also be a reference to a resource (in the form
171 | "@[package :]type :name
") or
172 | theme attribute (in the form
173 | "?[package :][type :]name
")
174 | containing a value of this type.
175 | @attr name com.melhc.simpleweather:title
176 | */
177 | public static final int Topball_title = 0;
178 | /**
179 |
This symbol is the offset where the {@link com.melhc.simpleweather.R.attr#titleTextColor}
180 | attribute's value can be found in the {@link #Topball} array.
181 |
182 |
183 |
Must be a color value, in the form of "#rgb
", "#argb
",
184 | "#rrggbb
", or "#aarrggbb
".
185 |
This may also be a reference to a resource (in the form
186 | "@[package :]type :name
") or
187 | theme attribute (in the form
188 | "?[package :][type :]name
")
189 | containing a value of this type.
190 | @attr name com.melhc.simpleweather:titleTextColor
191 | */
192 | public static final int Topball_titleTextColor = 2;
193 | /**
194 |
This symbol is the offset where the {@link com.melhc.simpleweather.R.attr#titleTextSize}
195 | attribute's value can be found in the {@link #Topball} array.
196 |
197 |
198 |
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp
".
199 | Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),
200 | in (inches), mm (millimeters).
201 |
This may also be a reference to a resource (in the form
202 | "@[package :]type :name
") or
203 | theme attribute (in the form
204 | "?[package :][type :]name
")
205 | containing a value of this type.
206 | @attr name com.melhc.simpleweather:titleTextSize
207 | */
208 | public static final int Topball_titleTextSize = 1;
209 | };
210 | }
211 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/libs/gson-2.2.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/libs/gson-2.2.4.jar
--------------------------------------------------------------------------------
/libs/litepal-1.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/libs/litepal-1.1.1.jar
--------------------------------------------------------------------------------
/libs/volley.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/libs/volley.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-hdpi/home.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-hdpi/logo.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-hdpi/refresh.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/melhc/SimpleWeather/9d358ba79c0300508a1a267743e2c3733c4d5ed1/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/choose_area.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
19 |
20 |
21 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/res/layout/choose_area_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/res/layout/weather_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
19 |
20 |
27 |
28 |
36 |
37 |
38 |
43 |
44 |
53 |
54 |
60 |
61 |
68 |
69 |
77 |
78 |
83 |
84 |
91 |
92 |
101 |
102 |
109 |
110 |
111 |
112 |
113 |
--------------------------------------------------------------------------------
/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
10 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 天气云
5 | Settings
6 | Hello world!
7 | -
8 |
9 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/com/melhc/activity/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.melhc.activity;
2 |
3 | import com.melhc.util.ActivityCollector;
4 | import com.melhc.util.LogUtil;
5 |
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 |
9 |
10 | public class BaseActivity extends Activity {
11 | /*
12 | * 自动管理活动的创建销毁
13 | *
14 | */
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | // TODO Auto-generated method stub
18 | super.onCreate(savedInstanceState);
19 | LogUtil.d("BaseActivity","----------->"+getClass().getSimpleName());
20 | ActivityCollector.addActivity(this);
21 | }
22 |
23 | @Override
24 | protected void onDestroy() {
25 | // TODO Auto-generated method stub
26 | super.onDestroy();
27 | ActivityCollector.removeActivity(this);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/melhc/activity/ChooseAreaActivity.java:
--------------------------------------------------------------------------------
1 | package com.melhc.activity;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import com.android.volley.RequestQueue;
7 | import com.android.volley.Response;
8 | import com.android.volley.VolleyError;
9 | import com.android.volley.toolbox.StringRequest;
10 | import com.android.volley.toolbox.Volley;
11 | import com.melhc.db.WeatherDB;
12 | import com.melhc.model.City;
13 | import com.melhc.model.County;
14 | import com.melhc.model.Province;
15 | import com.melhc.simpleweather.R;
16 | import com.melhc.util.LogUtil;
17 | import com.melhc.util.Utility;
18 |
19 | import android.app.ProgressDialog;
20 | import android.content.Intent;
21 | import android.content.SharedPreferences;
22 | import android.os.Bundle;
23 | import android.preference.PreferenceManager;
24 | import android.text.TextUtils;
25 | import android.view.View;
26 | import android.view.Window;
27 | import android.widget.AdapterView;
28 | import android.widget.AdapterView.OnItemClickListener;
29 | import android.widget.ArrayAdapter;
30 | import android.widget.ListView;
31 | import android.widget.TextView;
32 | import android.widget.Toast;
33 |
34 | public class ChooseAreaActivity extends BaseActivity {
35 | public static final int LEVEL_PROVINCE = 0;
36 | public static final int LEVEL_CITY = 1;
37 | public static final int LEVEL_COUNTY = 2;
38 | private ProgressDialog progressDialog;
39 | private TextView textView;
40 | private ListView listView;
41 | private ArrayAdapter adapter;
42 | private WeatherDB weatherDB;
43 | private List dataList = new ArrayList();
44 |
45 | private List proviceList;
46 |
47 | private List cityList;
48 |
49 | private List countyList;
50 |
51 | private Province selectedProvince;
52 |
53 | private City selectedCity;
54 |
55 | private int currentLevel;
56 |
57 | /**
58 | * 判断是否从WeatherActivity跳转过来
59 | */
60 | private boolean isFromWeatherActivity;
61 |
62 | @Override
63 | protected void onCreate(Bundle savedInstanceState) {
64 | // TODO Auto-generated method stub
65 | super.onCreate(savedInstanceState);
66 | isFromWeatherActivity = getIntent().getBooleanExtra(
67 | "from_weather_activity", false);
68 | SharedPreferences prefs = PreferenceManager
69 | .getDefaultSharedPreferences(getApplicationContext());
70 | if (prefs.getBoolean("city_selected", false) && !isFromWeatherActivity) {
71 | Intent intent = new Intent(this, WeatherActivity.class);
72 | startActivity(intent);
73 | finish();
74 | return;
75 | }
76 | requestWindowFeature(Window.FEATURE_NO_TITLE);
77 | setContentView(R.layout.choose_area);
78 | listView = (ListView) findViewById(R.id.list_view);
79 | textView = (TextView) findViewById(R.id.title_text);
80 | adapter = new ArrayAdapter(getApplicationContext(),
81 | R.layout.choose_area_item, R.id.textView1, dataList);
82 | listView.setAdapter(adapter);
83 | weatherDB = WeatherDB.getInstance();
84 | listView.setOnItemClickListener(new OnItemClickListener() {
85 |
86 | @Override
87 | public void onItemClick(AdapterView> parent, View view,
88 | int position, long id) {
89 | // TODO Auto-generated method stub
90 | if (currentLevel == LEVEL_PROVINCE) {
91 | selectedProvince = proviceList.get(position);
92 | queryCities();
93 | } else if (currentLevel == LEVEL_CITY) {
94 | selectedCity = cityList.get(position);
95 | queryCounties();
96 | } else if (currentLevel == LEVEL_COUNTY) {
97 | String countyCode = countyList.get(position)
98 | .getCounty_code();
99 | Intent intent = new Intent(ChooseAreaActivity.this,
100 | WeatherActivity.class);
101 | intent.putExtra("county_code", countyCode);
102 | startActivity(intent);
103 | finish();
104 | }
105 | }
106 |
107 | });
108 | queryProvinces();
109 | }
110 |
111 | /**
112 | * 查询全国的省,优先从数据库查询,如果没有查询再到服务器上查询
113 | */
114 | private void queryProvinces() {
115 | proviceList = weatherDB.loadProvices();
116 | if (proviceList.size() > 0) {
117 | dataList.clear();
118 | for (Province province : proviceList) {
119 | dataList.add(province.getProvince_name());
120 | }
121 | LogUtil.i("ChooseActivity",
122 | "--------------->" + dataList.toString());
123 | adapter.notifyDataSetChanged();
124 | listView.setSelection(0);
125 | textView.setText("中国");
126 | currentLevel = LEVEL_PROVINCE;
127 | } else {
128 | queryFromServer(null, "province");
129 | }
130 | }
131 |
132 | /**
133 | * 查询某省所有的市,优先从数据库查询,如果没有查询再到服务器上查询
134 | */
135 | private void queryCities() {
136 | cityList = weatherDB.loadCities(selectedProvince.getId());
137 | LogUtil.i("ChooseActivity", "--------------->" + cityList.toString());
138 | if (cityList.size() > 0) {
139 | dataList.clear();
140 | for (City city : cityList) {
141 | dataList.add(city.getCity_name());
142 | }
143 | LogUtil.i("ChooseActivity",
144 | "--------------->" + dataList.toString());
145 | adapter.notifyDataSetChanged();
146 | listView.setSelection(0);
147 | textView.setText(selectedProvince.getProvince_name());
148 | currentLevel = LEVEL_CITY;
149 | } else {
150 | queryFromServer(selectedProvince.getProvince_code(), "city");
151 | }
152 | }
153 |
154 | /**
155 | * 查询某市所有的县,优先从数据库查询,如果没有查询再到服务器上查询
156 | */
157 | private void queryCounties() {
158 | countyList = weatherDB.loadCounties(selectedCity.getId());
159 | if (countyList.size() > 0) {
160 | dataList.clear();
161 | for (County county : countyList) {
162 | dataList.add(county.getCounty_name());
163 | }
164 | adapter.notifyDataSetChanged();
165 | listView.setSelection(0);
166 | textView.setText(selectedCity.getCity_name());
167 | currentLevel = LEVEL_COUNTY;
168 | } else {
169 | // 从服务器查询County
170 | queryFromServer(selectedCity.getCity_code(), "county");
171 | }
172 | }
173 |
174 | /**
175 | * 根据传入的代号和数据查询省县市数据
176 | *
177 | */
178 | private void queryFromServer(final String code, final String type) {
179 | String address;
180 | if (!TextUtils.isEmpty(code)) {
181 | address = "http://www.weather.com.cn/data/list3/city" + code
182 | + ".xml";
183 | } else {
184 | address = "http://www.weather.com.cn/data/list3/city.xml";
185 | }
186 | showProgressDialog();
187 | RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
188 | StringRequest stringRequest = new StringRequest(address,
189 | new Response.Listener() {
190 | @Override
191 | public void onResponse(String response) {
192 | LogUtil.i("TAG", "---------------->"+response);
193 | boolean result = false;
194 | if ("province".equals(type)) {
195 | result = Utility.handleProvicesResponse(weatherDB,
196 | response);
197 | } else if ("city".equals(type)) {
198 | result = Utility.handleCitiesResponse(weatherDB,
199 | response, selectedProvince);
200 | } else if ("county".equals(type)) {
201 | result = Utility.handleCountiesResponse(weatherDB,
202 | response, selectedCity);
203 | }
204 | if (result) {
205 | // 通过runonUiMainThread方法返回主线程处理逻辑
206 | runOnUiThread(new Runnable() {
207 | @Override
208 | public void run() {
209 | // TODO Auto-generated method stub
210 | closeProgressDialog();
211 | if ("province".equals(type)) {
212 | queryProvinces();
213 | } else if ("city".equals(type)) {
214 | queryCities();
215 | } else if ("county".equals(type)) {
216 | queryCounties();
217 | }
218 | }
219 | });
220 | }
221 | }
222 | }, new Response.ErrorListener() {
223 | @Override
224 | public void onErrorResponse(VolleyError error) {
225 | LogUtil.i("TAG", "-------------------->" + error);
226 | runOnUiThread(new Runnable() {
227 | @Override
228 | public void run() { // TODO Auto-generated method
229 | // stub //
230 | closeProgressDialog();
231 | Toast.makeText(getApplicationContext(),
232 | "加载数据失败!", Toast.LENGTH_SHORT).show();
233 | }
234 | });
235 | }
236 | });
237 | mQueue.add(stringRequest);
238 | }
239 |
240 | /**
241 | * 显示进度对话框
242 | */
243 | private void showProgressDialog() {
244 | if (progressDialog == null) {
245 | progressDialog = new ProgressDialog(this);
246 | progressDialog.setTitle("天气云");
247 | progressDialog.setMessage("正在加载中....");
248 | progressDialog.setCanceledOnTouchOutside(false);
249 | }
250 | progressDialog.show();
251 | }
252 |
253 | /**
254 | * 关闭进度对话框
255 | */
256 | private void closeProgressDialog() {
257 | if (progressDialog != null) {
258 | progressDialog.dismiss();
259 | }
260 | }
261 |
262 | @Override
263 | public void onBackPressed() {
264 | // TODO Auto-generated method stub
265 | super.onBackPressed();
266 | if (currentLevel == LEVEL_COUNTY) {
267 | LogUtil.i("CHOOSEAREAACTIVITY", "------------->1");
268 | queryCities();
269 | LogUtil.i("CHOOSEAREAACTIVITY", "------------->2");
270 | } else if (currentLevel == LEVEL_CITY) {
271 | queryProvinces();
272 | } else {
273 | if (isFromWeatherActivity) {
274 | Intent intent = new Intent(this, WeatherActivity.class);
275 | startActivity(intent);
276 | }
277 | finish();
278 | LogUtil.i("CHOOSEAREAACTIVITY", "------------->3");
279 | }
280 | }
281 |
282 | @Override
283 | protected void onDestroy() {
284 | // TODO Auto-generated method stub
285 | super.onDestroy();
286 | // weatherDB.destroyDB();
287 | }
288 | }
289 |
--------------------------------------------------------------------------------
/src/com/melhc/activity/WeatherActivity.java:
--------------------------------------------------------------------------------
1 | package com.melhc.activity;
2 |
3 | import com.android.volley.RequestQueue;
4 | import com.android.volley.Response;
5 | import com.android.volley.VolleyError;
6 | import com.android.volley.toolbox.StringRequest;
7 | import com.android.volley.toolbox.Volley;
8 | import com.melhc.service.AutoUpdateService;
9 | import com.melhc.simpleweather.R;
10 | import com.melhc.util.LogUtil;
11 | import com.melhc.util.Utility;
12 |
13 | import android.content.Intent;
14 | import android.content.SharedPreferences;
15 | import android.os.Bundle;
16 | import android.preference.PreferenceManager;
17 | import android.text.TextUtils;
18 | import android.view.View;
19 | import android.view.View.OnClickListener;
20 | import android.view.Window;
21 | import android.widget.Button;
22 | import android.widget.LinearLayout;
23 | import android.widget.TextView;
24 |
25 | public class WeatherActivity extends BaseActivity implements OnClickListener {
26 | private LinearLayout weatherLinearLayout;
27 | /**
28 | * 用于显示城市名
29 | */
30 | private TextView cityNameText;
31 | /**
32 | * 用于显示发布时间
33 | */
34 | private TextView publishText;
35 | /**
36 | * 用于显示天气描述发布信息
37 | */
38 | private TextView weatherDespText;
39 | /**
40 | * 用于显示气温1
41 | */
42 | private TextView temp1Text;
43 | /**
44 | * 用于显示气温2
45 | */
46 | private TextView temp2Text;
47 | /**
48 | * 用于显示当前日期
49 | */
50 | private TextView currentDateText;
51 | /**
52 | * 切换城市的按钮
53 | */
54 | private Button switchCity;
55 | /**
56 | * 更新天气数据
57 | */
58 | private Button refreshWeather;
59 |
60 | @Override
61 | protected void onCreate(Bundle savedInstanceState) {
62 | // TODO Auto-generated method stub
63 | super.onCreate(savedInstanceState);
64 | requestWindowFeature(Window.FEATURE_NO_TITLE);
65 | setContentView(R.layout.weather_layout);
66 | initView();
67 | String countyCode = getIntent().getStringExtra("county_code");
68 | if (!TextUtils.isEmpty(countyCode)) {
69 | publishText.setText("同步中.....");
70 | weatherLinearLayout.setVisibility(View.INVISIBLE);
71 | cityNameText.setVisibility(View.INVISIBLE);
72 | queryWeatherCode(countyCode);
73 | } else {
74 | showWeather();
75 | }
76 | switchCity.setOnClickListener(this);
77 | refreshWeather.setOnClickListener(this);
78 | }
79 |
80 | /**
81 | * 初始化控件
82 | */
83 | public void initView() {
84 | weatherLinearLayout = (LinearLayout) findViewById(R.id.weather_info_layout);
85 | cityNameText = (TextView) findViewById(R.id.city_name);
86 | publishText = (TextView) findViewById(R.id.publish_text);
87 | weatherDespText = (TextView) findViewById(R.id.weather_desp);
88 | temp1Text = (TextView) findViewById(R.id.temp1);
89 | temp2Text = (TextView) findViewById(R.id.temp2);
90 | currentDateText = (TextView) findViewById(R.id.current_date);
91 | switchCity = (Button) findViewById(R.id.switch_city);
92 | refreshWeather = (Button) findViewById(R.id.refresh_weather);
93 | }
94 |
95 | @Override
96 | public void onClick(View v) {
97 | // TODO Auto-generated method stub
98 | switch (v.getId()) {
99 | case R.id.switch_city:
100 | Intent intent = new Intent(this, ChooseAreaActivity.class);
101 | intent.putExtra("from_weather_activity", true);
102 | startActivity(intent);
103 | finish();
104 | break;
105 | case R.id.refresh_weather:
106 | publishText.setText("同步中....");
107 | SharedPreferences preferences = PreferenceManager
108 | .getDefaultSharedPreferences(getApplicationContext());
109 | String weatherCode = preferences.getString("weather_code", "");
110 | if (!TextUtils.isEmpty(weatherCode)) {
111 | queryWeatherInfo(weatherCode);
112 | }
113 | break;
114 |
115 | default:
116 | break;
117 | }
118 | }
119 |
120 | /**
121 | * 查询县级代号所对应的天气代号
122 | */
123 | private void queryWeatherCode(String countyCode) {
124 | String address = "http://www.weather.com.cn/data/list3/city"
125 | + countyCode + ".xml";
126 | queryFromServer(address, "countyCode");
127 | }
128 |
129 | /**
130 | * 查询天气代号所对应的天气
131 | */
132 | private void queryWeatherInfo(String weatherCode) {
133 | String address = "http://www.weather.com.cn/data/cityinfo/"
134 | + weatherCode + ".html";
135 | LogUtil.i("TAG", "------------------->" + address);
136 | queryFromServer(address, "weatherCode");
137 | }
138 |
139 | /**
140 | * 根据传入的地址及类型去服务器端获取数据
141 | */
142 | private void queryFromServer(final String address, final String type) {
143 | RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
144 | StringRequest stringRequest = new StringRequest(address,
145 | new Response.Listener() {
146 | @Override
147 | public void onResponse(String response) {
148 | if ("countyCode".equals(type)) {
149 | if (!TextUtils.isEmpty(response)) {
150 | String[] array = response.split("\\|");
151 | if (array != null && array.length == 2) {
152 | String weatherCode = array[1];
153 | LogUtil.i("WeatherActivity",
154 | "-------------->" + weatherCode);
155 | queryWeatherInfo(weatherCode);
156 | }
157 | }
158 | } else if ("weatherCode".equals(type)) {
159 | // 处理服务器返回的天气信息
160 | Utility.handleWeatherResponse(
161 | getApplicationContext(), response);
162 | runOnUiThread(new Runnable() {
163 |
164 | @Override
165 | public void run() {
166 | // TODO Auto-generated method stub
167 | showWeather();
168 | }
169 | });
170 | }
171 |
172 | }
173 | }, new Response.ErrorListener() {
174 | @Override
175 | public void onErrorResponse(VolleyError error) {
176 | runOnUiThread(new Runnable() {
177 |
178 | @Override
179 | public void run() {
180 | // TODO Auto-generated method stub
181 | publishText.setText("同步失败");
182 | }
183 | });
184 |
185 | }
186 | });
187 | mQueue.add(stringRequest);
188 |
189 | }
190 |
191 | /**
192 | * 从共享参数中获取存储的天气信息,并显示到界面上
193 | */
194 | private void showWeather() {
195 | SharedPreferences prefs = PreferenceManager
196 | .getDefaultSharedPreferences(getApplicationContext());
197 | cityNameText.setText(prefs.getString("city_name", ""));
198 | temp1Text.setText(prefs.getString("temp1", ""));
199 | temp2Text.setText(prefs.getString("temp2", ""));
200 | weatherDespText.setText(prefs.getString("weather_desp", ""));
201 | publishText.setText("今天" + prefs.getString("publish_time", "") + "发布");
202 | currentDateText.setText(prefs.getString("current_date", ""));
203 | weatherLinearLayout.setVisibility(View.VISIBLE);
204 | cityNameText.setVisibility(View.VISIBLE);
205 | Intent intent = new Intent(this, AutoUpdateService.class);
206 | startService(intent);
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/src/com/melhc/db/WeatherDB.java:
--------------------------------------------------------------------------------
1 | package com.melhc.db;
2 |
3 | import java.util.List;
4 |
5 | import org.litepal.crud.DataSupport;
6 | import org.litepal.tablemanager.Connector;
7 |
8 | import com.melhc.model.City;
9 | import com.melhc.model.County;
10 | import com.melhc.model.Province;
11 |
12 | import android.database.sqlite.SQLiteDatabase;
13 |
14 | public class WeatherDB {
15 | /**
16 | * 一些基本的数据库方法封装
17 | */
18 | private SQLiteDatabase db;
19 |
20 | private static WeatherDB weatherDB;
21 |
22 | public WeatherDB() {
23 | // TODO Auto-generated constructor stub
24 | db = Connector.getDatabase();//正式生成数据库
25 | }
26 |
27 | public synchronized static WeatherDB getInstance() {
28 | if (weatherDB == null) {
29 | weatherDB = new WeatherDB();
30 | }
31 | return weatherDB;
32 | }
33 |
34 | /**
35 | * 将provice实例存储到数据库
36 | */
37 | public void saveProvice(Province province) {
38 | if (province != null) {
39 | province.save();
40 | }
41 | }
42 |
43 | /**
44 | * 从数据库读取全国所有的省份信息
45 | */
46 | public List loadProvices() {
47 | List list = DataSupport.findAll(Province.class);
48 | return list;
49 | }
50 |
51 | /**
52 | * 将city实例存储到数据库
53 | */
54 | public void saveCity(City city) {
55 | if (city != null) {
56 | city.save();
57 | }
58 | }
59 |
60 | /**
61 | * 从数据库读取某省下的所有的城市信息
62 | */
63 | public List loadCities(int provinceId) {
64 | Province provice = DataSupport.find(Province.class, provinceId,true);
65 | List list = provice.getCities();
66 |
67 | return list;
68 | }
69 |
70 | /**
71 | * 将county实例存储到数据库
72 | */
73 | public void saveCounty(County county) {
74 | if (county != null) {
75 | county.save();
76 | }
77 | }
78 |
79 | /**
80 | * 从数据库读取某城市下的所有的县信息
81 | */
82 | public List loadCounties(int cityId) {
83 | City city = DataSupport.find(City.class, cityId,true);
84 | List list = city.getCounties();
85 | return list;
86 | }
87 |
88 | /**
89 | * 关闭数据库
90 | */
91 | public void destroyDB() {
92 | if (db != null) {
93 | db.close();
94 | }
95 | }
96 |
97 | }
98 |
--------------------------------------------------------------------------------
/src/com/melhc/model/City.java:
--------------------------------------------------------------------------------
1 | package com.melhc.model;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.litepal.crud.DataSupport;
7 |
8 | public class City extends DataSupport {
9 | private int id;
10 | private String city_name;
11 | private String city_code;
12 | private Province province;
13 |
14 | private List counties = new ArrayList();
15 |
16 | public List getCounties() {
17 |
18 | return counties;
19 | }
20 |
21 | public void setCounties(List counties) {
22 | this.counties = counties;
23 | }
24 |
25 | public int getId() {
26 | return id;
27 | }
28 |
29 | public void setId(int id) {
30 | this.id = id;
31 | }
32 |
33 | public String getCity_name() {
34 | return city_name;
35 | }
36 |
37 | public void setCity_name(String city_name) {
38 | this.city_name = city_name;
39 | }
40 |
41 | public String getCity_code() {
42 | return city_code;
43 | }
44 |
45 | public void setCity_code(String city_code) {
46 | this.city_code = city_code;
47 | }
48 |
49 | public Province getProvince() {
50 | return province;
51 | }
52 |
53 | public void setProvince(Province province) {
54 | this.province = province;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/com/melhc/model/County.java:
--------------------------------------------------------------------------------
1 | package com.melhc.model;
2 |
3 | import org.litepal.crud.DataSupport;
4 |
5 | public class County extends DataSupport {
6 | private int id;
7 | private String county_name;
8 | private String county_code;
9 | private City city;
10 |
11 | public int getId() {
12 | return id;
13 | }
14 |
15 | public void setId(int id) {
16 | this.id = id;
17 | }
18 |
19 | public String getCounty_name() {
20 | return county_name;
21 | }
22 |
23 | public void setCounty_name(String county_name) {
24 | this.county_name = county_name;
25 | }
26 |
27 | public String getCounty_code() {
28 | return county_code;
29 | }
30 |
31 | public void setCounty_code(String county_code) {
32 | this.county_code = county_code;
33 | }
34 |
35 | public City getCity() {
36 | return city;
37 | }
38 |
39 | public void setCity(City city) {
40 | this.city = city;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/com/melhc/model/Province.java:
--------------------------------------------------------------------------------
1 | package com.melhc.model;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.litepal.crud.DataSupport;
7 |
8 | public class Province extends DataSupport {
9 | private int id;
10 | private String province_name;
11 |
12 | private String province_code;
13 |
14 | private List cities = new ArrayList();
15 |
16 | public String getProvince_code() {
17 | return province_code;
18 | }
19 |
20 | public void setProvince_code(String province_code) {
21 | this.province_code = province_code;
22 | }
23 |
24 | public List getCities() {
25 |
26 | return cities;
27 | }
28 |
29 | public void setCities(List cities) {
30 | this.cities = cities;
31 | }
32 |
33 | public int getId() {
34 | return id;
35 | }
36 |
37 | public void setId(int id) {
38 | this.id = id;
39 | }
40 |
41 | public String getProvince_name() {
42 | return province_name;
43 | }
44 |
45 | public void setProvince_name(String province_name) {
46 | this.province_name = province_name;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/com/melhc/model/Weather.java:
--------------------------------------------------------------------------------
1 | package com.melhc.model;
2 |
3 | public class Weather {
4 | private Weatherinfo weatherinfo;
5 |
6 | public Weatherinfo getWeatherinfo() {
7 | return weatherinfo;
8 | }
9 |
10 | public void setWeatherInfo(Weatherinfo weatherinfo) {
11 | this.weatherinfo = weatherinfo;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/melhc/model/WeatherInfo.java:
--------------------------------------------------------------------------------
1 | package com.melhc.model;
2 |
3 |
4 |
5 | public class Weatherinfo {
6 | private String city;
7 | private String cityid;
8 | private String temp1;
9 | private String temp2;
10 | private String weather;
11 | private String ptime;
12 | public String getCity() {
13 | return city;
14 | }
15 | public void setCity(String city) {
16 | this.city = city;
17 | }
18 | public String getCityid() {
19 | return cityid;
20 | }
21 | public void setCityid(String cityid) {
22 | this.cityid = cityid;
23 | }
24 | public String getTemp1() {
25 | return temp1;
26 | }
27 | public void setTemp1(String temp1) {
28 | this.temp1 = temp1;
29 | }
30 | public String getTemp2() {
31 | return temp2;
32 | }
33 | public void setTemp2(String temp2) {
34 | this.temp2 = temp2;
35 | }
36 | public String getWeather() {
37 | return weather;
38 | }
39 | public void setWeather(String weather) {
40 | this.weather = weather;
41 | }
42 | public String getPtime() {
43 | return ptime;
44 | }
45 | public void setPtime(String ptime) {
46 | this.ptime = ptime;
47 | }
48 | @Override
49 | public String toString() {
50 | return "WeatherInfo [city=" + city + ", cityid=" + cityid + ", temp1="
51 | + temp1 + ", temp2=" + temp2 + ", weather=" + weather
52 | + ", ptime=" + ptime + "]";
53 | }
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/com/melhc/reciever/AutoUpdateReceiver.java:
--------------------------------------------------------------------------------
1 | package com.melhc.reciever;
2 |
3 | import com.melhc.service.AutoUpdateService;
4 |
5 | import android.content.BroadcastReceiver;
6 | import android.content.Context;
7 | import android.content.Intent;
8 |
9 | public class AutoUpdateReceiver extends BroadcastReceiver {
10 |
11 | @Override
12 | public void onReceive(Context context, Intent intent) {
13 | // TODO Auto-generated method stub
14 | Intent i = new Intent(context, AutoUpdateService.class);
15 | context.startService(i);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/melhc/service/AutoUpdateService.java:
--------------------------------------------------------------------------------
1 | package com.melhc.service;
2 |
3 | import com.android.volley.RequestQueue;
4 | import com.android.volley.Response;
5 | import com.android.volley.VolleyError;
6 | import com.android.volley.toolbox.StringRequest;
7 | import com.android.volley.toolbox.Volley;
8 | import com.melhc.reciever.AutoUpdateReceiver;
9 | import com.melhc.util.LogUtil;
10 | import com.melhc.util.Utility;
11 |
12 | import android.app.AlarmManager;
13 | import android.app.PendingIntent;
14 | import android.app.Service;
15 | import android.content.Intent;
16 | import android.content.SharedPreferences;
17 | import android.os.IBinder;
18 | import android.os.SystemClock;
19 | import android.preference.PreferenceManager;
20 |
21 | public class AutoUpdateService extends Service {
22 |
23 | @Override
24 | public IBinder onBind(Intent intent) {
25 | // TODO Auto-generated method stub
26 | return null;
27 | }
28 |
29 | @Override
30 | public int onStartCommand(Intent intent, int flags, int startId) {
31 | // TODO Auto-generated method stub
32 | new Thread(new Runnable() {
33 |
34 | @Override
35 | public void run() {
36 | // TODO Auto-generated method stub
37 | // updateWeather();
38 | }
39 | }).start();
40 | AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
41 | int anHour = 8 * 60 * 60 * 1000;
42 | long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
43 | Intent i = new Intent(this, AutoUpdateReceiver.class);
44 | PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
45 | manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
46 |
47 | return super.onStartCommand(intent, flags, startId);
48 | }
49 |
50 | /**
51 | * 更新天气
52 | */
53 | public void updeteWeather() {
54 | SharedPreferences prefs = PreferenceManager
55 | .getDefaultSharedPreferences(getApplicationContext());
56 | String weather_code = prefs.getString("weather_code", "");
57 | String address = "http:www.weather.com.cn/data/cityinfo/"
58 | + weather_code + ".html";
59 | RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
60 | StringRequest stringRequest = new StringRequest(address,
61 | new Response.Listener() {
62 | @Override
63 | public void onResponse(String response) {
64 | Utility.handleWeatherResponse(getApplicationContext(),
65 | response);
66 |
67 | }
68 | }, new Response.ErrorListener() {
69 | @Override
70 | public void onErrorResponse(VolleyError error) {
71 | LogUtil.i("TAG", "-------------------->" + error);
72 |
73 | }
74 | });
75 | mQueue.add(stringRequest);
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/src/com/melhc/util/ActivityCollector.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import android.app.Activity;
7 | /*
8 | * 活动管理器
9 | */
10 | public class ActivityCollector {
11 | //初始化活动集合
12 | public static List activities = new ArrayList();
13 | //添加新的活动
14 | public static void addActivity(Activity activity) {
15 | //先判断list集合里是否已有该活动
16 | if(!activities.contains(activity)){
17 | activities.add(activity);
18 | }
19 |
20 | }
21 | //移除指定的活动
22 | public static void removeActivity(Activity activity) {
23 | activities.remove(activity);
24 | }
25 | //移除所有的活动
26 | public static void finishAll() {
27 | for (Activity activity : activities) {
28 | if (!activity.isFinishing()) {
29 | activity.finish();
30 | }
31 | }
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/com/melhc/util/HttpCallbackListener.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | /*
4 | * 网路连接的回掉接口
5 | */
6 | public interface HttpCallbackListener {
7 | void onFinish(String response);
8 |
9 | void onError(Exception e);
10 | }
11 |
--------------------------------------------------------------------------------
/src/com/melhc/util/HttpUtil.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.InputStream;
5 | import java.io.InputStreamReader;
6 | import java.net.HttpURLConnection;
7 | import java.net.URL;
8 | public class HttpUtil {
9 | /*
10 | * 从服务器端获取省县市的数据
11 | */
12 | public static void sendHttpRequest(final String address,
13 | final HttpCallbackListener listener) {
14 | new Thread(new Runnable() {
15 | @Override
16 | public void run() {
17 | // TODO Auto-generated method stub
18 | HttpURLConnection connection = null;
19 | try {
20 | //创建一个url对象
21 | URL url = new URL(address);
22 | //通过url对象获取HTTPURLCONNCETION实例
23 | connection = (HttpURLConnection) url.openConnection();
24 | //设置http的请求所使用的方法为get方法
25 | connection.setRequestMethod("GET");
26 | //自由定制一些属性,比如设置连接超时,读取超时的毫秒数
27 | connection.setConnectTimeout(8000);
28 | connection.setReadTimeout(8000);
29 | //获得服务器返回的输入流
30 | InputStream in = connection.getInputStream();
31 | //将得到的输入流装换成String字符串
32 | BufferedReader reader = new BufferedReader(
33 | new InputStreamReader(in, "utf-8"));
34 | StringBuffer response = new StringBuffer();
35 | String line;
36 | while ((line = reader.readLine()) != null) {
37 | response.append(line);
38 | }
39 | LogUtil.i("HTTPUtil",
40 | "------------------>" + response.toString());
41 | if (listener != null) {
42 | listener.onFinish(response.toString());
43 | }
44 | } catch (Exception e) {
45 | // TODO: handle exception
46 | if (listener != null) {
47 | listener.onError(e);
48 | }
49 | } finally {
50 | if (connection != null) {
51 | connection.disconnect();
52 | }
53 | }
54 | }
55 | }).start();
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/com/melhc/util/LogUtil.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | import android.util.Log;
4 |
5 | public class LogUtil {
6 | public static final int VERBOSE = 1;
7 |
8 | public static final int DEBUG = 2;
9 |
10 | public static final int INFO = 3;
11 |
12 | public static final int WARN = 4;
13 |
14 | public static final int ERROR = 5;
15 |
16 | public static final int NOTHING = 6;
17 |
18 | public static final int LEVEAL = VERBOSE;
19 |
20 | public static void v(String tag, String msg) {
21 | if (LEVEAL <= VERBOSE) {
22 | Log.v(tag, msg);
23 | }
24 | }
25 |
26 | public static void d(String tag, String msg) {
27 | if (LEVEAL <= DEBUG) {
28 | Log.v(tag, msg);
29 | }
30 | }
31 |
32 | public static void i(String tag, String msg) {
33 | if (LEVEAL <= INFO) {
34 | Log.v(tag, msg);
35 | }
36 | }
37 |
38 | public static void w(String tag, String msg) {
39 | if (LEVEAL <= WARN) {
40 | Log.v(tag, msg);
41 | }
42 | }
43 |
44 | public static void e(String tag, String msg) {
45 | if (LEVEAL <= ERROR) {
46 | Log.v(tag, msg);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/com/melhc/util/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | import android.content.Context;
4 |
5 | public class MyApplication extends org.litepal.LitePalApplication {
6 | /*
7 | * 构建Application管理全局变量
8 | */
9 |
10 | private static Context context;
11 |
12 | @Override
13 | public void onCreate() {
14 | // TODO Auto-generated method stub
15 | super.onCreate();
16 | context = getApplicationContext();
17 | }
18 |
19 | public static Context getContext() {
20 | return context;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/com/melhc/util/TopBar.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | import com.melhc.simpleweather.R;
4 |
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.drawable.Drawable;
8 | import android.util.AttributeSet;
9 | import android.view.Gravity;
10 | import android.view.ViewGroup;
11 | import android.widget.Button;
12 | import android.widget.RelativeLayout;
13 | import android.widget.TextView;
14 | /**
15 | * 自定义的顶端topbar布局
16 | * @author Melhc0623
17 | *
18 | */
19 | public class TopBar extends RelativeLayout {
20 | private Button leftButton,rightButton;
21 | private TextView tvText;
22 | private Drawable leftBackground,rightBackground;
23 | private float titleTextSize;
24 | private int titleTextColor;
25 | private String title;
26 | private LayoutParams leftParams,rightParams,centerParams;
27 | @SuppressWarnings("deprecation")
28 | public TopBar(Context context, AttributeSet attrs) {
29 | super(context, attrs);
30 | // TODO Auto-generated constructor stub
31 | TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.Topball);
32 | leftBackground=typedArray.getDrawable(R.styleable.Topball_leftBackground);
33 | rightBackground=typedArray.getDrawable(R.styleable.Topball_rightBackground);
34 | titleTextSize=typedArray.getDimension(R.styleable.Topball_titleTextSize, 0);
35 | titleTextColor=typedArray.getColor(R.styleable.Topball_titleTextColor, 0);
36 | title=typedArray.getString(R.styleable.Topball_title);
37 | typedArray.recycle();
38 | leftButton=new Button(context);
39 | rightButton=new Button(context);
40 | tvText=new TextView(context);
41 | //该方法已经不推荐使用,仅为了向下兼容版本,使android 2.0的用户也能使用我们的软件
42 | leftButton.setBackgroundDrawable(leftBackground);
43 | rightButton.setBackgroundDrawable(rightBackground);
44 | tvText.setText(title);
45 | tvText.setTextColor(titleTextColor);
46 | tvText.setTextSize(titleTextSize);
47 | tvText.setGravity(Gravity.CENTER);
48 |
49 | leftParams=new LayoutParams(30,30);
50 | leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
51 | leftParams.setMargins(10, 0, 0, 0);
52 | addView(leftButton, leftParams);
53 |
54 | rightParams=new LayoutParams(30,30);
55 | rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
56 | rightParams.setMargins(0, 0, 10, 0);
57 | addView(rightButton, rightParams);
58 |
59 | centerParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
60 | centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
61 | addView(tvText, centerParams);
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/com/melhc/util/Utility.java:
--------------------------------------------------------------------------------
1 | package com.melhc.util;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 | import java.util.Locale;
6 |
7 |
8 | import com.google.gson.Gson;
9 | import com.melhc.db.WeatherDB;
10 | import com.melhc.model.City;
11 | import com.melhc.model.County;
12 | import com.melhc.model.Province;
13 | import com.melhc.model.Weather;
14 | import com.melhc.model.Weatherinfo;
15 |
16 | import android.content.Context;
17 | import android.content.SharedPreferences;
18 | import android.preference.PreferenceManager;
19 | import android.text.TextUtils;
20 |
21 | public class Utility {
22 | /**
23 | * 处理服务端返回的省级json数据
24 | */
25 | public synchronized static boolean handleProvicesResponse(
26 | WeatherDB weatherDB, String response) {
27 | if (!TextUtils.isEmpty(response)) {
28 | String[] allProvices = response.split(",");
29 | if (allProvices != null && allProvices.length > 0) {
30 | for (String p : allProvices) {
31 | String[] array = p.split("\\|");
32 | Province province = new Province();
33 | province.setProvince_code(array[0]);
34 |
35 | province.setProvince_name(array[1]);
36 |
37 | weatherDB.saveProvice(province);
38 | }
39 | return true;
40 | }
41 | }
42 |
43 | return false;
44 | }
45 |
46 | /**
47 | * 处理服务端返回的市级json数据
48 | */
49 | public synchronized static boolean handleCitiesResponse(
50 | WeatherDB weatherDB, String response, Province province) {
51 |
52 | if (!TextUtils.isEmpty(response)) {
53 | String[] allCities = response.split(",");
54 | if (allCities != null && allCities.length > 0) {
55 | for (String c : allCities) {
56 | String[] array = c.split("\\|");
57 | City city = new City();
58 | city.setCity_code(array[0]);
59 | city.setCity_name(array[1]);
60 | city.setProvince(province);
61 | weatherDB.saveCity(city);
62 | }
63 | return true;
64 | }
65 | }
66 | return false;
67 | }
68 |
69 | /**
70 | * 处理服务端返回的县级json数据
71 | */
72 | public synchronized static boolean handleCountiesResponse(
73 | WeatherDB weatherDB, String response, City city) {
74 | if (!TextUtils.isEmpty(response)) {
75 | String[] allCounties = response.split(",");
76 | if (allCounties != null && allCounties.length > 0) {
77 | for (String c : allCounties) {
78 | String[] array = c.split("\\|");
79 | County county = new County();
80 | county.setCounty_code(array[0]);
81 | county.setCounty_name(array[1]);
82 | county.setCity(city);
83 | weatherDB.saveCounty(county);
84 | }
85 | return true;
86 | }
87 | }
88 | return false;
89 | }
90 |
91 | /**
92 | * 使用Gson解析服务器返回的JSON数据,并将解析的数据存储到本地
93 | */
94 | public static void handleWeatherResponse(Context context, String response) {
95 | try {
96 | LogUtil.i("UTILITY", "----------------->" + response.toString());
97 | Gson gson = new Gson();
98 | Weather weather = gson.fromJson(response, Weather.class);
99 | LogUtil.i("UTILITY", "----------------->" + weather.toString());
100 | Weatherinfo info = weather.getWeatherinfo();
101 | LogUtil.i("UTILITY", "----------------->" + info.toString());
102 | saveWeatherInfo(context, info);
103 | } catch (Exception e) {
104 | // TODO: handle exception
105 | }
106 | }
107 |
108 | /**
109 | * 将服务器返回的天气信息存储到共享参数中
110 | */
111 | public static void saveWeatherInfo(Context context, Weatherinfo info) {
112 |
113 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA);
114 | SharedPreferences.Editor editor = PreferenceManager
115 | .getDefaultSharedPreferences(context).edit();
116 | editor.putBoolean("city_selected", true);
117 | editor.putString("city_name", info.getCity());
118 | editor.putString("weather_code", info.getCityid());
119 | editor.putString("temp1", info.getTemp1());
120 | editor.putString("temp2", info.getTemp2());
121 | editor.putString("weather_desp", info.getWeather());
122 | editor.putString("publish_time", info.getPtime());
123 | LogUtil.i("UTILITY", "----------------->" + sdf.format(new Date()));
124 | editor.putString("current_date", sdf.format(new Date()));
125 | editor.commit();
126 | }
127 | }
128 |
--------------------------------------------------------------------------------