本文主要是介绍酷欧天气项目,追加定位功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
整个工程的源码我也放在公众号里了,回复
CoolWeather 即可获取。
1.罗列全国各省,市,县;
2.可以查看全国各城市天气
3.自由切换城市,随意查看
4.提供手动更新,以及后台更新天气的功能。
5.定位出自己所在位置
主要用拥有的界面有这些:
由于需要定位,这里我使用的是百度的SDK,需要在百度开发者平台注册,下载下来的文档我放在了公众号里 ,在公众号中回复定位SDK即可!
文档包括这几项:
要将.jar文件复制到androidstudio的libs目录下,再在src/main下新建一个包,命名为jniLibs,再将剩下的文件全部复制到这个目录下,这其中可能会有的文件复制不进去,是因为这里面有其他语言的包,不要紧。
在AndroidManifest中添加权限,代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.coolweather"><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.WAKE_LOCK" /><applicationandroid:name="org.litepal.LitePalApplication"android:allowBackup="true"android:icon="@mipmap/icon"android:label="@string/app_name"android:networkSecurityConfig="@xml/network_security_config"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><meta-dataandroid:name="com.baidu.lbsapi.API_KEY"android:value="WQHscCyQNrpp73by617PsFLWXjC0NPpv" />//这是在百度LBS 中申请到的API Key,<service //后台更新天气的服务android:name=".AutoUpdateService"android:enabled="true"android:exported="true" /><activity android:name=".WeatherActivity" /><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity><service //处理百度LBS SDK 中混淆的代码android:name="com.baidu.location.f"android:enabled="true"android:process=":remote"></service></application></manifest>
接下来在app/build.gradle中添加依赖:
dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation 'com.android.support:appcompat-v7:28.0.0'implementation 'com.android.support.constraint:constraint-layout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'implementation 'com.squareup.okhttp3:okhttp:3.4.1'//网络请求implementation 'com.google.code.gson:gson:2.7'//gson解析数据implementation 'com.github.bumptech.glide:glide:3.7.0'//glide加载图片implementation 'org.litepal.android:core:1.4.1'//litepal数据库
}
添加了最下面4条依赖
下面,在com.example.coolweather下新建几个包,
db包终于存放数据库模型相关代码,gson包存放gson模型相关代码,service中存放服务相关代码,util包存放工具相关代码。
下面准备建立3张表:province,city,county,分别用于存放省,市,县,
对应的在db包下建立3个类,Province,City,County类,代码如下:
import org.litepal.crud.DataSupport;public class Province extends DataSupport {private int id;//每个实体类应有的字段private String provinceName;//记录省的名字,private int provinceCode;//记录省的代号
//public int getId() {return id;}public void setId(int id) {this.id = id;}public String getProvinceName() {return provinceName;}public void setProvinceName(String provinceName) {this.provinceName = provinceName;}public int getProvinceCode() {return provinceCode;}public void setProvinceCode(int provinceCode) {this.provinceCode = provinceCode;}
}
import org.litepal.crud.DataSupport;public class City extends DataSupport {private int id;private String cityName;//城市名字private int cityCode;//市的代号private int provinceId;//当前市所属省的idpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getCityName() {return cityName;}public void setCityName(String cityName) {this.cityName = cityName;}public int getCityCode() {return cityCode;}public void setCityCode(int cityCode) {this.cityCode = cityCode;}public int getProvinceId() {return provinceId;}public void setProvinceId(int provinceId) {this.provinceId = provinceId;}
}
import org.litepal.crud.DataSupport;public class County extends DataSupport {private int id;private String countyName;//县的名字private String weatherId;//天气idprivate int cityId;//所属市的idpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getCountyName() {return countyName;}public void setCountyName(String countyName) {this.countyName = countyName;}public String getWeatherId() {return weatherId;}public void setWeatherId(String weatherId) {this.weatherId = weatherId;}public int getCityId() {return cityId;}public void setCityId(int cityId) {this.cityId = cityId;}
}
建好表后,再在app/src/main目录下新建一个包,命名为assets,在目录下新建一个litepal.xml的文件,建立之后它会不在这个目录下,将它移到这个目录下,然后编辑其中内容:
<litepal><dbname value="cool_weather"/><version value ="1"/><list><mapping class="com.example.coolweather.db.Province"/>//将3个实体类添加到映射列表中<mapping class="com.example.coolweather.db.City"/><mapping class="com.example.coolweather.db.County"/></list>
</litepal>
再在AndroidManifest中添加一天依赖,上面给出时已经添加,
为 android:name=“org.litepal.LitePalApplication”;
需要从服务器中获取所有省市县的数据,因此与服务器的交互必不可少,因此,在util包下新建一个HttpUtil类,代码如下:
package com.example.coolweather.util;import okhttp3.OkHttpClient;
import okhttp3.Request;public class HttpUtil {public static void sendOkHttpRequest(String address,okhttp3.Callback callback){OkHttpClient client=new OkHttpClient(); //创建OkHttpClient的实例Request request=new Request.Builder().url(address).build();//创建Request 对象,在其中传入address 地址,client.newCall(request).enqueue(callback);//注册一个回调来处理服务器的响应}
}
由于从服务器中返回的数据都是JSON格式的,因此需要提供一个工具类来解析个处理这种数据,在util包下新建一个Utility类代码如下:
package com.example.coolweather.util;
import android.text.TextUtils;
import com.example.coolweather.db.City;
import com.example.coolweather.db.County;
import com.example.coolweather.db.Province;
import com.example.coolweather.gson.Weather;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;public class Utility {//解析处理服务器返回的省级数据public static boolean handleProvinceResponse(String response){if(!TextUtils.isEmpty(response)){try {JSONArray allProvinces =new JSONArray(response);for(int i=0;i<allProvinces.length();i++){JSONObject provinceObject=allProvinces.getJSONObject(i);Province province=new Province();province.setProvinceName(provinceObject.getString("name"));province.setProvinceCode(provinceObject.getInt("id"));province.save();}return true;} catch (JSONException e) {e.printStackTrace();}}return false;}//解析处理服务器返回的市级数据public static boolean handleCityResponse(String response,int provinceId){if(!TextUtils.isEmpty(response)){try {JSONArray allCities =new JSONArray(response);for(int i=0;i<allCities.length();i++){JSONObject cityObject=allCities.getJSONObject(i);City city=new City();city.setCityName(cityObject.getString("name"));city.setCityCode(cityObject.getInt("id"));city.setProvinceId(provinceId);city.save();}return true;} catch (JSONException e) {e.printStackTrace();}}return false;}
//解析处理服务器返回的县级数据public static boolean handleCountyResponse(String response,int cityId) {if(!TextUtils.isEmpty(response)){try {JSONArray allCounties =new JSONArray(response);for(int i=0;i<allCounties.length();i++){JSONObject countyObject=allCounties.getJSONObject(i);County county=new County();county.setCountyName(countyObject.getString("name"));county.setWeatherId(countyObject.getString("weather_id"));county.setCityId(cityId);county.save();}return true;} catch (JSONException e) {e.printStackTrace();}}return false;}//在后天要添加处理天气数据的方法,这里提前给出,后面还会介绍public static Weather handleWeatherResponse(String response){try {JSONObject jsonObject=new JSONObject(response);JSONArray jsonArray=jsonObject.getJSONArray("HeWeather");String weatherContent=jsonArray.getJSONObject(0).toString();return new Gson().fromJson(weatherContent,Weather.class);} catch (JSONException e) {e.printStackTrace();}return null;}
}
下面开始写布局,由于省市县的功能在后面还要用到做滑动菜单,因此,在这里就不写在活动里了,而是写在碎片里,在res/layout下新建choose_area.xml的布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#fff"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"><TextViewandroid:id="@+id/title_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:textColor="#fff"android:textSize="20sp"/><Buttonandroid:id="@+id/back_button"android:layout_width="25dp"android:layout_height="25dp"android:layout_marginLeft="10dp"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:background="@drawable/apple"/></RelativeLayout><ListViewandroid:id="@+id/list_view"android:layout_width="match_parent"android:layout_height="match_parent"></ListView>
</LinearLayout>
接下来需要写出这个碎片的java 代码:
package com.example.coolweather;import android.app.ProgressDialog;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;import com.example.coolweather.db.City;
import com.example.coolweather.db.County;
import com.example.coolweather.db.Province;
import com.example.coolweather.util.HttpUtil;
import com.example.coolweather.util.Utility;import org.litepal.crud.DataSupport;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;public class ChooseAreaFragment extends Fragment {public static final int LEVEL_PROVINCE = 0;//分别代表当前所处的位置public static final int LEVEL_CITY = 1;public static final int LEVEL_COUNTY = 2;private ProgressDialog progressDialog; //加载中显示页面private TextView titleText;//标题private Button backButton;//返回键private ListView listView;//省市县加载显示private ArrayAdapter<String> adapter;//list view的适配器private List<String> dataList = new ArrayList<>();//数组,用于存放list view中的省市县数据,以便初始化适配器private List<Province> provinceList;//省列表private List<City> cityList;//市列表private List<County> countyList;县列表private Province selectedProvince;//选中的省份private City selectedCity;//选中的城市private int currentLevel;//当前页面的级别@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.choose_area, container, false);titleText = view.findViewById(R.id.title_text);backButton = view.findViewById(R.id.back_button);listView = view.findViewById(R.id.list_view);adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, dataList);//初始化适配器listView.setAdapter(adapter);return view;}@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //listview中item的点击事件@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {if (currentLevel == LEVEL_PROVINCE) {selectedProvince = provinceList.get(position);queryCities();} else if (currentLevel == LEVEL_CITY) {selectedCity = cityList.get(position);queryCounties();}else if(currentLevel==LEVEL_COUNTY){ //此段为后面追加String weatherId=countyList.get(position).getWeatherId();if(getActivity()instanceof MainActivity){Intent intent=new Intent(getActivity(),WeatherActivity.class);intent.putExtra("weather_id",weatherId);startActivity(intent);getActivity().finish();}else if(getActivity()instanceof WeatherActivity){WeatherActivity activity= (WeatherActivity) getActivity();activity.drawerLayout.closeDrawers();activity.swipeRefresh.setRefreshing(true);activity.requestWeather(weatherId);}}}});backButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (currentLevel == LEVEL_COUNTY) {queryCities();} else if (currentLevel == LEVEL_CITY) {queryProvinces();}}});queryProvinces();}
//查询全国的省,优先从数据库中查找,如果没有就到服务器中查询private void queryProvinces() {titleText.setText("中国");backButton.setVisibility(View.GONE);//不显示返回键provinceList = DataSupport.findAll(Province.class);if (provinceList.size() > 0) {dataList.clear();for (Province province : provinceList) {dataList.add(province.getProvinceName());}adapter.notifyDataSetChanged();//刷新适配器内容listView.setSelection(0);保持第0个数据在第一行currentLevel = LEVEL_PROVINCE;} else {String address = "http://guolin.tech/api/china";queryFromServer(address, "province");}}private void queryCities() {titleText.setText(selectedProvince.getProvinceName());backButton.setVisibility(View.VISIBLE);//显示返回键cityList = DataSupport.where("provinceid=?", String.valueOf(selectedProvince.getId())).find(City.class);if (cityList.size() > 0) {dataList.clear();for (City city : cityList) {dataList.add(city.getCityName());}adapter.notifyDataSetChanged();listView.setSelection(0);currentLevel = LEVEL_CITY;} else {int provinceCode = selectedProvince.getProvinceCode();String address = "http://guolin.tech/api/china/" + provinceCode;queryFromServer(address, "city");}}private void queryCounties() {titleText.setText(selectedCity.getCityName());backButton.setVisibility(View.VISIBLE);countyList = DataSupport.where("cityid=?", String.valueOf(selectedCity.getId())).find(County.class);if (countyList.size() > 0) {dataList.clear();for (County county : countyList) {dataList.add(county.getCountyName());}adapter.notifyDataSetChanged();listView.setSelection(0);currentLevel = LEVEL_COUNTY;} else {int provinceCode = selectedProvince.getProvinceCode();int cityCode = selectedCity.getCityCode();String address = "http://guolin.tech/api/china/" + provinceCode + "/" + cityCode;queryFromServer(address, "county");}}//根据传入的地址和类型从服务器上查询省市县数据private void queryFromServer(String address, final String type) {showProgressDialog();HttpUtil.sendOkHttpRequest(address, new Callback() {@Overridepublic void onResponse(Call call, Response response) throws IOException {String responseText=response.body().string();boolean result=false;if("province".equals(type)){result= Utility.handleProvinceResponse(responseText);}else if("city".equals(type)){result=Utility.handleCityResponse(responseText,selectedProvince.getId());}else if("county".equals(type)){result=Utility.handleCountyResponse(responseText,selectedCity.getId());}if(result){getActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {closeProgressDialog();if("province".equals(type)){queryProvinces();}else if("city".equals(type)){queryCities();}else if("county".equals(type)){queryCounties();}}});}}@Overridepublic void onFailure(Call call, IOException e) {getActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {closeProgressDialog();Toast.makeText(getContext(),"加载失败",Toast.LENGTH_SHORT).show();}});}});}private void showProgressDialog() {if (progressDialog == null) {progressDialog = new ProgressDialog(getActivity());progressDialog.setMessage("正在加载");progressDialog.setCanceledOnTouchOutside(false);//点击屏幕不消失,按返回键消失,但是setCancelable(false)方法则点击屏幕不消失,按返回键也不消失}progressDialog.show();}private void closeProgressDialog() {if (progressDialog != null) {progressDialog.dismiss();}}
}
到这里只需要将碎片添加到activity_main中。
修改一下activity_main代码,如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/choose_area_fragment"android:name="com.example.coolweather.ChooseAreaFragment"android:layout_width="match_parent"android:layout_height="match_parent" />
</FrameLayout>
另外,碎片布局已经有了自定义的标题栏,因此就不再需要原生的ActionBar,修改res/values/styles.xml中代码:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
到这里已经可以显示省市县了,接下来写天气界面。
在写天气界面之前,先要处理服务器返回的天气的json数据,由于这个数据比较复杂,用JSONObject解析起来会很复杂,因此这里用GOSN来解析。
在gson包中建立Basic,AQI,Now,Suggestion,Forecast类,再创建一个实例类,将刚刚创建的实体类引用,Weather类;
package com.example.coolweather.gson;import com.google.gson.annotations.SerializedName;public class Basic {@SerializedName("city")public String cityName;@SerializedName("id")public String weatherId;public Update update;public class Update {@SerializedName("loc")public String updateTime;}
}
package com.example.coolweather.gson;public class AQI {public AQICity city;public class AQICity {public String aqi;public String pm25;}
}
package com.example.coolweather.gson;import com.google.gson.annotations.SerializedName;public class Now {@SerializedName("tmp")public String temperature;@SerializedName("cond")public More more;public class More{@SerializedName("txt")public String info;}
}
package com.example.coolweather.gson;import com.google.gson.annotations.SerializedName;public class Suggestion {@SerializedName("comf")public Comfort comfort;@SerializedName("cw")public CarWash carWash;public Sport sport;public class Comfort {@SerializedName("txt")public String info;}public class CarWash {@SerializedName("txt")public String info;}public class Sport {@SerializedName("txt")public String info;}
}
package com.example.coolweather.gson;import com.google.gson.annotations.SerializedName;public class Forecast {public String date;@SerializedName("tmp")public Temperature temperature;@SerializedName("cond")public More more;public class Temperature{public String max;public String min;}public class More {@SerializedName("txt_d")public String info;}
}
Weather类引用上述类
package com.example.coolweather.gson;import com.google.gson.annotations.SerializedName;import java.util.List;public class Weather {public String status;//返回成功为OK,失败为失败原因public Basic basic;public AQI aqi;public Now now;public Suggestion suggestion;@SerializedName("daily_forecast")public List<Forecast> forecastList;
}
现在在Utility类中添加处理天气JSON数据的方法,上面给出代码中已经提前给出了,这里再给一次:
//上面是处理省市县数据的方法public static Weather handleWeatherResponse(String response) {try {JSONObject jsonObject = new JSONObject(response);JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");String weatherContent = jsonArray.getJSONObject(0).toString();return new Gson().fromJson(weatherContent, Weather.class);} catch (JSONException e) {e.printStackTrace();}return null;}
接下来就是天气页面的布局了:
由于天气布局控件比较多,因此这里用引入布局的方法来实现:
新建布局,title.xml,now.xml,forecast.xml,forecast_item.xml,aqi.xml,
suggestion.xml,再建一个WeatherActivity的空活动,在activity_weather.xml
中引入上述布局。
title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"><Button//滑动菜单,后面添加的android:id="@+id/nav_button"android:layout_marginLeft="10dp"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:background="@drawable/apple"android:layout_width="30dp"android:layout_height="30dp" /><TextViewandroid:id="@+id/title_city"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:textColor="#fff"android:textSize="20sp"/><TextViewandroid:id="@+id/title_update_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:textSize="16sp"android:textColor="#fff"/>
</RelativeLayout>
now.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="15dp"><TextView//定位,后面添加android:id="@+id/position"android:textColor="#fff"android:gravity="center"android:textSize="20sp"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/degree_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="end"android:textColor="#fff"android:textSize="60sp" /><TextViewandroid:id="@+id/weather_info_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="end"android:textColor="#fff"android:textSize="20sp" /></LinearLayout>
</LinearLayout>
forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_margin="15dp"android:background="#8000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:layout_marginTop="15dp"android:text="预报"android:textColor="#fff"android:textSize="20sp"/><LinearLayoutandroid:id="@+id/forecast_layout"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"></LinearLayout></LinearLayout>
forecast_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="15dp"><TextViewandroid:id="@+id/date_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"android:layout_gravity="center_vertical"android:textColor="#fff"/><TextViewandroid:id="@+id/info_text"android:layout_gravity="center_vertical"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:textColor="#fff"/><TextViewandroid:id="@+id/max_text"android:layout_gravity="center"android:gravity="right"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textColor="#fff"/><TextViewandroid:id="@+id/min_text"android:layout_gravity="center"android:gravity="right"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textColor="#fff"/>
</LinearLayout>
aqi.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="15dp"android:background="#8000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:layout_marginTop="15dp"android:text="空气质量"android:textColor="#fff"android:textSize="20sp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="15dp"><RelativeLayoutandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"><LinearLayoutandroid:orientation="vertical"android:layout_centerInParent="true"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/aqi_text"android:layout_gravity="center"android:textColor="#fff"android:textSize="40sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_gravity="center"android:text="AQI指数"android:textColor="#fff"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></RelativeLayout><RelativeLayoutandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"><LinearLayoutandroid:orientation="vertical"android:layout_centerInParent="true"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/pm25_text"android:layout_gravity="center"android:textColor="#fff"android:textSize="40sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_gravity="center"android:text="PM2.5指数"android:textColor="#fff"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></RelativeLayout></LinearLayout>
</LinearLayout>
sugestion.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="15dp"android:background="#8000"><TextViewandroid:layout_marginLeft="15dp"android:layout_marginTop="15dp"android:text="生活建议"android:textColor="#fff"android:textSize="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/comfort_text"android:layout_margin="15dp"android:textColor="#fff"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/car_wash_text"android:layout_margin="15dp"android:textColor="#fff"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/sport_text"android:layout_margin="15dp"android:textColor="#fff"android:layout_width="wrap_content"android:layout_height="wrap_content" />
</LinearLayout>
接下来将这些布局引入activity_weather中:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPrimary"><ImageViewandroid:id="@+id/bing_pic_img"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="centerCrop"/><android.support.v4.widget.DrawerLayout//滑动菜单android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.widget.SwipeRefreshLayout//下拉刷新android:id="@+id/swipe_refresh"android:layout_width="match_parent"android:layout_height="match_parent"><ScrollViewandroid:id="@+id/weather_layout"android:scrollbars="none"android:overScrollMode="never"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:fitsSystemWindows="true"><include layout="@layout/title"/><include layout="@layout/now"/><include layout="@layout/forecast"/><include layout="@layout/aqi"/><include layout="@layout/suggestion"/></LinearLayout></ScrollView></android.support.v4.widget.SwipeRefreshLayout><fragment//滑动菜单的碎片android:id="@+id/choose_area_fragment"android:name="com.example.coolweather.ChooseAreaFragment"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="start"/></android.support.v4.widget.DrawerLayout>
</FrameLayout>
WeatherActivity中代码:
package com.example.coolweather;import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bumptech.glide.Glide;
import com.example.coolweather.gson.Forecast;
import com.example.coolweather.gson.Weather;
import com.example.coolweather.service.AutoUpdateService;
import com.example.coolweather.util.HttpUtil;
import com.example.coolweather.util.Utility;import java.io.IOException;import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;public class WeatherActivity extends AppCompatActivity {private TextView positionText;//定位显示public LocationClient mLocationClient;//定位实现public DrawerLayout drawerLayout;//滑动菜单private Button navButton;//启动滑动菜单的按钮public SwipeRefreshLayout swipeRefresh;//下拉刷新private String mWeatherId;private ImageView bingPicImg;//背景图片private ScrollView weatherLayout;//滑动显示区private LinearLayout forecastLayout;//近几天天气情况private TextView titleCity,titleUpdateTime,degreeText,weatherInfoText,aqiText,pm25Text,comfortText,carWashText,sportText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mLocationClient=new LocationClient(getApplicationContext());mLocationClient.registerLocationListener(new MyLocationListener());//位置监听器if(Build.VERSION.SDK_INT>=21) {//背景图与状态栏融合,对SDK版本有要求View decorView = getWindow().getDecorView();decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);getWindow().setStatusBarColor(Color.TRANSPARENT);}setContentView(R.layout.activity_weather);positionText=findViewById(R.id.position);weatherLayout=findViewById(R.id.weather_layout);titleCity=findViewById(R.id.title_city);titleUpdateTime=findViewById(R.id.title_update_time);degreeText=findViewById(R.id.degree_text);weatherInfoText=findViewById(R.id.weather_info_text);forecastLayout=findViewById(R.id.forecast_layout);aqiText=findViewById(R.id.aqi_text);pm25Text=findViewById(R.id.pm25_text);comfortText=findViewById(R.id.comfort_text);carWashText=findViewById(R.id.car_wash_text);sportText=findViewById(R.id.sport_text);bingPicImg=findViewById(R.id.bing_pic_img);swipeRefresh=findViewById(R.id.swipe_refresh);swipeRefresh.setColorSchemeResources(R.color.colorPrimary);drawerLayout=findViewById(R.id.drawer_layout);navButton=findViewById(R.id.nav_button);navButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {drawerLayout.openDrawer(GravityCompat.START);//打开滑动菜单}});SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);String weatherString=prefs.getString("weather",null);//从数据库中查找天气if(weatherString!=null){//如果有,则直接解析显示Weather weather= Utility.handleWeatherResponse(weatherString);mWeatherId=weather.basic.weatherId;showWeatherInfo(weather);}else{//如果没有,则网络请求加载天气mWeatherId=getIntent().getStringExtra("weather_id");weatherLayout.setVisibility(View.INVISIBLE);requestWeather(mWeatherId);}swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {requestWeather(mWeatherId);//下拉请求重新加载天气}});String bingPic=prefs.getString("bing_pic",null);//数据库中查找背景图if(bingPic!=null){Glide.with(this).load(bingPic).into(bingPicImg);//如果有则用Glide加载}else{loadBingPic();//如果没有则调用此方法网络请求加载}}public void requestWeather(final String weatherId) {//刷新天气String weatherUrl="http://guolin.tech/api/weather?cityid="+weatherId+"&key=bc0418b57b2d4918819d3974ac1285d9";HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {@Overridepublic void onFailure(Call call, IOException e) {e.printStackTrace();runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(WeatherActivity.this, "获取天气信息失败", Toast.LENGTH_SHORT).show();swipeRefresh.setRefreshing(false);}});}@Overridepublic void onResponse(Call call, Response response) throws IOException {final String responseText=response.body().string();final Weather weather=Utility.handleWeatherResponse(responseText);runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(WeatherActivity.this,"haha",Toast.LENGTH_SHORT).show();requestLocation();if(weather!=null&&"ok".equals(weather.status)){SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();editor.putString("weather",responseText);editor.apply();//天气保存到数据库mWeatherId=weather.basic.weatherId;showWeatherInfo(weather);}else{Toast.makeText(WeatherActivity.this, "获取天气信息错误", Toast.LENGTH_SHORT).show();}swipeRefresh.setRefreshing(false);//关闭下拉菜单}});}});loadBingPic();}private void loadBingPic() {//加载图片的方法String requestBingPic="http://guolin.tech/api/bing_pic";HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {@Overridepublic void onFailure(Call call, IOException e) {e.printStackTrace();}@Overridepublic void onResponse(Call call, Response response) throws IOException {final String bingPic=response.body().string();SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();editor.putString("bing_pic",bingPic);editor.apply();runOnUiThread(new Runnable() {@Overridepublic void run() {Glide.with(WeatherActivity.this).load(bingPic).into(bingPicImg);}});}});}private void showWeatherInfo(Weather weather) {//显示天气信息String cityName=weather.basic.cityName;String updateTime=weather.basic.update.updateTime.split(" ")[1];//加载用空格隔开的json数据的第2个数据String degree=weather.now.temperature+"℃";String weatherInfo=weather.now.more.info;titleCity.setText(cityName);titleUpdateTime.setText(updateTime);degreeText.setText(degree);weatherInfoText.setText(weatherInfo);forecastLayout.removeAllViews();for(Forecast forecast:weather.forecastList){View view= LayoutInflater.from(this).inflate(R.layout.forecast_item,forecastLayout,false);TextView dateText=view.findViewById(R.id.date_text);TextView infoText=view.findViewById(R.id.info_text);TextView maxText=view.findViewById(R.id.max_text);TextView minText=view.findViewById(R.id.min_text);dateText.setText(forecast.date);infoText.setText(forecast.more.info);maxText.setText(forecast.temperature.max);minText.setText(forecast.temperature.min);forecastLayout.addView(view);}if(weather.aqi!=null){aqiText.setText(weather.aqi.city.aqi);pm25Text.setText(weather.aqi.city.pm25);}String comfort="舒适度"+weather.suggestion.comfort.info;String carWash="洗车指数"+weather.suggestion.carWash.info;String sport="运动建议"+weather.suggestion.sport.info;comfortText.setText(comfort);carWashText.setText(carWash);sportText.setText(sport);requestLocation();weatherLayout.setVisibility(View.VISIBLE);Intent intent=new Intent(this, AutoUpdateService.class);//开启后台更新服务startService(intent);}public class MyLocationListener implements BDLocationListener{@Overridepublic void onReceiveLocation(final BDLocation location) {runOnUiThread(new Runnable() {@Overridepublic void run() {StringBuilder currentPosition=new StringBuilder();currentPosition.append(location.getDistrict());positionText.setText(currentPosition);Toast.makeText(WeatherActivity.this,"djjfjhsj",Toast.LENGTH_SHORT).show();}});}}public void requestLocation(){initLocation();mLocationClient.start();//响应监听器}public void initLocation(){LocationClientOption option=new LocationClientOption();option.setIsNeedAddress(true);mLocationClient.setLocOption(option);}@Overrideprotected void onDestroy() {super.onDestroy();mLocationClient.stop();}
}
新建后台服务的代码 在Service下新建一个服务,命名为AutoUpdateService,将Exported,Enabled都勾上,代码如下:
package com.example.coolweather.service;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;import com.example.coolweather.gson.Weather;
import com.example.coolweather.util.HttpUtil;
import com.example.coolweather.util.Utility;import java.io.IOException;import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;public class AutoUpdateService extends Service {@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {updateWeather();//更新天气界面updateBingPic();//更新图片AlarmManager manager= (AlarmManager) getSystemService(ALARM_SERVICE);int anHour =5*1000;//定时5秒long triggerAtTime= SystemClock.elapsedRealtime()+anHour;Intent i=new Intent(this,AutoUpdateService.class);PendingIntent pi=PendingIntent.getService(this,0,i,0);manager.cancel(pi);manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);return super.onStartCommand(intent, flags, startId);}private void updateWeather() {SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);String weatherString =prefs.getString("weather",null);if(weatherString!=null){Weather weather= Utility.handleWeatherResponse(weatherString);String weatherId=weather.basic.weatherId;String weatherUrl="http://guolin.tech/api/weather?cityid="+weatherId+"&key=bc0418b57b2d4918819d3974ac1285d9";HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {@Overridepublic void onFailure(Call call, IOException e) {e.printStackTrace();}@Overridepublic void onResponse(Call call, Response response) throws IOException {String responseText=response.body().string();Weather weather=Utility.handleWeatherResponse(responseText);if(weather!=null&&"ok".equals(weather.status)){SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();editor.putString("weather",responseText);editor.apply();}}});}}private void updateBingPic() {String requestBingPic="http://guolin.tech/api/bing_pic";HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {@Overridepublic void onFailure(Call call, IOException e) {e.printStackTrace();}@Overridepublic void onResponse(Call call, Response response) throws IOException {String bingPic=response.body().string();SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();editor.putString("bing_pic",bingPic);editor.apply();}});}}
工程到这里差不多就结束了,整个工程的源码我也放在公众号里了,回复
CoolWeather 即可获取。
感谢大家观看。
这篇关于酷欧天气项目,追加定位功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!