代码.docx
- 文档编号:23399243
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:63
- 大小:31.74KB
代码.docx
《代码.docx》由会员分享,可在线阅读,更多相关《代码.docx(63页珍藏版)》请在冰豆网上搜索。
代码
主程序
packagecom.way.weather;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.http.HttpResponse;
importorg.apache.http.HttpStatus;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importandroid.app.Dialog;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.support.v4.app.Fragment;
importandroid.support.v4.app.FragmentActivity;
importandroid.support.v4.view.ViewPager;
importandroid.text.TextUtils;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.ProgressBar;
importandroid.widget.TextView;
importcom.baidu.location.BDLocation;
importcom.baidu.location.BDLocationListener;
importcom.baidu.location.LocationClient;
importcom.google.gson.Gson;
importcom.google.gson.GsonBuilder;
importcom.google.gson.reflect.TypeToken;
importcom.way.apapter.WeatherPagerAdapter;
importcom.way.app.Application;
importcom.way.bean.City;
importcom.way.bean.Pm2d5;
importcom.way.bean.SimpleWeather;
importcom.way.bean.SimpleWeatherinfo;
importcom.way.bean.Weather;
importcom.way.bean.Weatherinfo;
importcom.way.db.CityDB;
importcom.way.fragment.FirstWeatherFragment;
importcom.way.fragment.SecondWeatherFragment;
importcom.way.indicator.CirclePageIndicator;
importcom.way.util.ConfigCache;
importcom.way.util.IphoneDialog;
importcom.way.util.L;
importcom.way.util.NetUtil;
importcom.way.util.SharePreferenceUtil;
importcom.way.util.T;
importcom.way.util.TimeUtil;
publicclassMainActivityextendsFragmentActivityimplements
Application.EventHandler,OnClickListener{
publicstaticfinalStringUPDATE_WIDGET_WEATHER_ACTION="com.way.action.update_weather";
publicstaticfinalStringWEATHER_SIMPLE_URL="简要天气信息
publicstaticfinalStringWEATHER_BASE_URL="详细天气
publicstaticfinalStringPM2D5_BASE_URL=
"http:
//www.pm25.in/api/querys/pm2_5.json?
city=SHENZHEN&token=HUL7sQBaUKVvgWzdKdTB&stations=no";
privatestaticfinalStringWEATHER_INFO_FILENAME="_weather.json";
privatestaticfinalStringSIMPLE_WEATHER_INFO_FILENAME="_simple_weather.json";
privatestaticfinalStringPM2D5_INFO_FILENAME="_pm2d5.json";
privatestaticfinalintLOACTION_OK=0;
privatestaticfinalintON_NEW_INTENT=1;
privatestaticfinalintUPDATE_EXISTS_CITY=2;
privatestaticfinalintGET_WEATHER_RESULT=3;
privateLocationClientmLocationClient;
privateCityDBmCityDB;
privateSharePreferenceUtilmSpUtil;
privateApplicationmApplication;
privateCitymCurCity;
privateWeatherinfomCurWeatherinfo;
privateSimpleWeatherinfomCurSimpleWeatherinfo;
privatePm2d5mCurPm2d5;
privateGsonmGson;
privateImageViewmCityManagerBtn,mUpdateBtn,mLocationBtn,mShareBtn;
privateProgressBarmUpdateProgressBar;
privateTextViewmTitleTextView;
privateCitymNewIntentCity;
privateWeatherPagerAdaptermWeatherPagerAdapter;
privateTextViewcityTv,timeTv,humidityTv,weekTv,pmDataTv,pmQualityTv,
temperatureTv,climateTv,windTv;
privateImageViewweatherImg,pmImg;;
privateViewPagermViewPager;
privateList
privateHandlermHandler=newHandler(){
publicvoidhandleMessage(android.os.Messagemsg){
switch(msg.what){
caseLOACTION_OK:
StringcityName=(String)msg.obj;
L.i("cityName="+cityName);
mCurCity=mCityDB.getCity(cityName);
L.i(mCurCity.toString());
mSpUtil.setCity(mCurCity.getCity());
cityTv.setText(mCurCity.getCity());
updateWeather(true);
break;
caseON_NEW_INTENT:
mCurCity=mNewIntentCity;
mSpUtil.setCity(mCurCity.getCity());
cityTv.setText(mCurCity.getCity());
updateWeather(true);
break;
caseUPDATE_EXISTS_CITY:
StringsPCityName=mSpUtil.getCity();
mCurCity=mCityDB.getCity(sPCityName);
updateWeather(false);
break;
caseGET_WEATHER_RESULT:
updateWeatherInfo();
updatePm2d5Info();
updateWidgetWeather();
mUpdateBtn.setVisibility(View.VISIBLE);
mUpdateProgressBar.setVisibility(View.GONE);
break;
default:
break;
}
}
};
privatevoidupdateWidgetWeather(){
sendBroadcast(newIntent(UPDATE_WIDGET_WEATHER_ACTION));
}
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
privatevoidstartActivityForResult(){
Intenti=newIntent(this,SelectCtiyActivity.class);
startActivityForResult(i,0);
}
privatevoidinitView(){
mCityManagerBtn=(ImageView)findViewById(R.id.title_city_manager);
mUpdateBtn=(ImageView)findViewById(R.id.title_update_btn);
mShareBtn=(ImageView)findViewById(R.id.title_share);
mLocationBtn=(ImageView)findViewById(R.id.title_location);
mCityManagerBtn.setOnClickListener(this);
mUpdateBtn.setOnClickListener(this);
mShareBtn.setOnClickListener(this);
mLocationBtn.setOnClickListener(this);
mShareBtn.setVisibility(View.GONE);
mUpdateProgressBar=(ProgressBar)findViewById(R.id.title_update_progress);
mTitleTextView=(TextView)findViewById(R.id.title_city_name);
cityTv=(TextView)findViewById(R.id.city);
timeTv=(TextView)findViewById(R.id.time);
timeTv.setText(TimeUtil.getDay(mSpUtil.getTimeSamp())
+mSpUtil.getTime()+"发布");
humidityTv=(TextView)findViewById(R.id.humidity);
weekTv=(TextView)findViewById(R.id.week_today);
weekTv.setText("今天"+TimeUtil.getWeek(0,TimeUtil.XING_QI));
pmDataTv=(TextView)findViewById(R.id.pm_data);
pmQualityTv=(TextView)findViewById(R.id.pm2_5_quality);
pmImg=(ImageView)findViewById(R.id.pm2_5_img);
temperatureTv=(TextView)findViewById(R.id.temperature);
climateTv=(TextView)findViewById(R.id.climate);
windTv=(TextView)findViewById(R.id.wind);
weatherImg=(ImageView)findViewById(R.id.weather_img);
fragments=newArrayList
fragments.add(newFirstWeatherFragment());
fragments.add(newSecondWeatherFragment());
mViewPager=(ViewPager)findViewById(R.id.viewpager);
mWeatherPagerAdapter=newWeatherPagerAdapter(
getSupportFragmentManager(),fragments);
mViewPager.setAdapter(mWeatherPagerAdapter);
((CirclePageIndicator)findViewById(R.id.indicator))
.setViewPager(mViewPager);
if(TextUtils.isEmpty(mSpUtil.getCity())){
if(NetUtil.getNetworkState(this)!
=NetUtil.NETWORN_NONE){
mLocationClient.start();
mLocationClient.requestLocation();
T.showShort(this,"正在定位...");
mUpdateBtn.setVisibility(View.GONE);
mUpdateProgressBar.setVisibility(View.VISIBLE);
}else{
T.showShort(this,R_err);
}
}else{
mHandler.sendEmptyMessage(UPDATE_EXISTS_CITY);
}
}
privatevoidinitData(){
Application.mListeners.add(this);
mApplication=Application.getInstance();
mSpUtil=mApplication.getSharePreferenceUtil();
mLocationClient=mApplication.getLocationClient();
mLocationClient.registerLocationListener(mLocationListener);
mCityDB=mApplication.getCityDB();
//不转换没有@Expose注解的字段
mGson=newGsonBuilder().excludeFieldsWithoutExposeAnnotation()
.create();
}
privatevoidupdateWeather(finalbooleanisRefresh){
if(NetUtil.getNetworkState(this)==NetUtil.NETWORN_NONE&&isRefresh){
T.showLong(this,R_err);
return;
}
if(mCurCity==null){
T.showLong(mApplication,"未找到此城市,请重新定位或选择...");
return;
}
//T.showShort(this,"正在刷新天气...");
timeTv.setText("同步中...");
mTitleTextView.setText(mCurCity.getCity()+"天气");
mUpdateBtn.setVisibility(View.GONE);
mUpdateProgressBar.setVisibility(View.VISIBLE);
//启动线程获取天气信息
newThread(){
@Override
publicvoidrun(){
super.run();
getWeatherInfo(isRefresh);
getSimpleWeatherInfo(isRefresh);
getPm2d5Info(isRefresh);
if(mCurSimpleWeatherinfo!
=null)
L.i(mCurSimpleWeatherinfo.toString());
if(mCurWeatherinfo!
=null)
L.i(mCurWeatherinfo.toString());
if(mCurPm2d5!
=null)
L.i(mCurPm2d5.toString());
mHandler.sendEmptyMessage(GET_WEATHER_RESULT);
}
}.start();
}
privatevoidgetWeatherInfo(booleanisRefresh){
Stringurl=WEATHER_BASE_URL+mCurCity.getNumber()+".html";
Stringresult;
if(!
isRefresh){
if(mApplication.getmCurWeatherinfo()!
=null){//读取内存中的信息
mCurWeatherinfo=mApplication.getmCurWeatherinfo();
L.i("gettheweatherinfofrommemory");
return;//直接返回,不继续执行
}
//result=getInfoFromFile(WEATHER_INFO_FILENAME);//文件中的信息
result=ConfigCache.getUrlCache(url);
if(!
TextUtils.isEmpty(result)){
parseWeatherInfo(url,result,false);
L.i("gettheweatherinfofromfile");
return;
}
}
//L.i("weatherurl:
"+url);
StringweatherResult=connServerForResult(url);
if(TextUtils.isEmpty(weatherResult))
weatherResult=getInfoFromFile(WEATHER_INFO_FILENAME);
parseWeatherInfo(url,weatherResult,true);
}
privatevoidgetSimpleWeatherInfo(booleanisRefresh){
Stringurl=WEATHER_SIMPLE_URL+mCurCity.getNumber()+".html";
Stringresult;
if(!
isRefresh){
if(mApplication.getCurSimpleWeatherinfo()!
=null){//读取内存中的信息
mCurSimpleWeatherinfo=mApplication.getCurSimpleWeatherinfo();
L.i("getthesimpleweatherinfofrommemory");
return;//直接返回,不继续执行
}
//result=getInfoFromFile(SIMPLE_WEATHER_INFO_FILENAME);//文件中的信息
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)