package com.jiuqi.njt.ui;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.jqyd.android.module.lbs.LocationClient;
import com.jqyd.android.module.lbs.Interface.ILocationListener;
import com.jqyd.android.module.lbs.bean.LocationInfo;
import com.jqyd.android.module.lbs.util.WriteFile;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.search.core.LatLonPoint;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.CheckStateInterface;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.register.city.CityBean;
import com.jiuqi.njt.register.city.WeatherXzqhActivity;
import com.jiuqi.njt.ui.weather.GetWeatherNewTaskNew;
import com.jiuqi.njt.ui.weather.GetWeatherNewTaskNew.ReturnWeatherData;
import com.jiuqi.njt.ui.weather.WeatherBean;
import com.jiuqi.njt.util.AMapUtil;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.UIUtil;
import com.jiuqi.njt.widget.XzqhActivityNew;
/**
* 新的天气预报页面
* 进入页面是先定位,定位之后拿到省市,通过省市查询天气数据
* 选择行政区划的时候是通过选择的行政区划编号查的天气
* com.jiuqi.njt.ui.weather.GetWeatherNewTaskNew
* @author Joe
*/
public class WearthActivityNew extends BaseActivity implements
AMapLocationListener, OnClickListener {
private MyApp application;
private OptsharepreInterface sharePre;
private SharedPreferences weatherShare;
private LocationManagerProxy mAMapLocManager;
private String province;
private String city;
private View load_Layout;
private View weatherLayout;
private TextView oneDateTV;
private TextView oneWeatherTV;
private TextView twoDateTV;
private TextView twoWeatherTV;
private TextView threeDateTV;
private TextView threeWeatherTV;
private TextView fourDateTV;
private TextView fourWeatherTV;
private Button optBtn;
private Button refreshBtn;
private TextView weatherCelsiusTV;
private TextView weatherTV;
private TextView weatherCloudTV;
private TextView weatherDateTV;
private boolean flag;
private SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
public static final String YYYY_MM_DD_HH_MM = "MM月dd日 EEEE";
private final static String TAG = "WearthActivityNew";
private WriteFile writeFile = new WriteFile(TAG);
private WeatherBean weather;
private TextView weatherTempterTV;
private View middlelayout;
private CityBean cityBean; // 自定义的天气行政区划实体
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
Intent in = getIntent();
flag = in.getBooleanExtra("flag", false);// 天气推送过来的数据,就直接展示
if (flag) {
/**
* 获取到传递过来的天气数据
*/
weather = (WeatherBean) in.getSerializableExtra("WeatherBean");
} else {
enableMyLocation();
}
// width = UIUtil.getScreenWidth(WearthActivityNew.this);
weatherShare = getSharedPreferences("weather", 0);
application = (MyApp) getApplication();
sharePre = new OptsharepreInterface(this);
}
private void initWidgets() {
setContentView(R.layout.wearth_show_activity_new);
optBtn = (Button) findViewById(R.id.weatherOptBtn);
refreshBtn = (Button) findViewById(R.id.weatherRefreshBtn);
weatherLayout = findViewById(R.id.weatherLayout);
load_Layout = findViewById(R.id.load_Layout);
middlelayout = findViewById(R.id.middlelayout);
weatherCelsiusTV = (TextView) findViewById(R.id.dqwd_wearth);
weatherTempterTV = (TextView) findViewById(R.id.weatherTempterTV);
weatherTV = (TextView) findViewById(R.id.dqtq_wearth);
weatherCloudTV = (TextView) findViewById(R.id.sd_wearth);
weatherDateTV = (TextView) findViewById(R.id.rq_wearth);
oneDateTV = (TextView) findViewById(R.id.weatherOneDateTV);
oneWeatherTV = (TextView) findViewById(R.id.weatherOneWeatherTV);
twoDateTV = (TextView) findViewById(R.id.weatherTwoDateTV);
twoWeatherTV = (TextView) findViewById(R.id.weatherTwoWeatherTV);
threeDateTV = (TextView) findViewById(R.id.weatherThreeDateTV);
threeWeatherTV = (TextView) findViewById(R.id.weatherThreeWeatherTV);
fourDateTV = (TextView) findViewById(R.id.weatherFourDateTV);
fourWeatherTV = (TextView) findViewById(R.id.weatherFourWeatherTV);
}
private void initListeners() {
optBtn.setOnClickListener(this);
refreshBtn.setOnClickListener(this);
}
private void initUI() {
if (null!=application.getCity()&&!TextUtils.isEmpty(application.getCity())) {
optBtn.setText(" " + (application.getCity()) + " ");
}
if (flag) {
province = weather.getProCity();
city = weather.getCity();
setUIData(weather);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.weatherOptBtn:
Intent in = new Intent();
in.setClass(WearthActivityNew.this, WeatherXzqhActivity.class);
in.putExtra(Constants.WEATHER_PARAM_XZQH, cityBean);
startActivityForResult(in, Constants.REQUEST_XZQH_CODE);
break;
case R.id.weatherRefreshBtn:
getWeather(null != cityBean,true);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_XZQH_CODE) {
if (resultCode == RESULT_OK) {
cityBean = (CityBean) data
.getSerializableExtra(Constants.WEATHER_PARAM_XZQH);
if (null != cityBean) {
province = cityBean.getCityId();
city = cityBean.getCountyName();
getWeather(true,true);
}
}
}
}
/**
* 开始定位
*/
public void enableMyLocation() {
if (mAMapLocManager == null) {
mAMapLocManager = LocationManagerProxy
.getInstance(WearthActivityNew.this);
}
// 判断是否有网络,没有不请求,不上报
// Location API定位采用GPS和网络混合定位方式,时间最短是5000毫秒
CheckStateInterface checkStateInterface = new CheckStateInterface(
WearthActivityNew.this);
if (checkStateInterface.checkConnection()) {
// mAMapLocManager.requestLocationUpdates(
// LocationProviderProxy.AMapNetwork, 5000, 1,
// WearthActivityNew.this);
setUpBaiduLocation();
}
}
/**
* 结束定位
*/
public void disableMyLocation() {
if (mAMapLocManager != null) {
mAMapLocManager.removeUpdates(this);
mAMapLocManager.destory();
}
mAMapLocManager = null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final AMapLocation location) {
handler.sendEmptyMessage(2);
Message msg = new Message();
if (location != null) {
Double geoLat = location.getLatitude();
Double geoLng = location.getLongitude();
String cityCode = "";
String desc = "";
StringBuffer sb = new StringBuffer();
Bundle locBundle = location.getExtras();
if (locBundle != null) {
cityCode = locBundle.getString("citycode");
desc = locBundle.getString("desc");
sb.append("定位方式:").append(location.getProvider());
// sb.append("\n定位成功:(").append(geoLng).append(",").append(geoLat).append(")");
sb.append("\n精 度 :").append(location.getAccuracy())
.append("米");
sb.append("\n定位时间:").append(
AMapUtil.convertToTime(location.getTime()));
sb.append("\n城市编码:").append(cityCode);
sb.append("\n位置描述:").append(desc);
sb.append("\n省:").append(location.getProvince());
sb.append("\n市:").append(location.getCity());
sb.append("\n区(县):").append(location.getDistrict());
sb.append("\n城市编码:").append(location.getCityCode());
sb.append("\n区域编码:").append(location.getAdCode());
sb.append("\n详细经纬度" + geoLat + "-------" + geoLng);
msg.getData().putDouble("latitude", geoLat);
msg.getData().putDouble("longitude", geoLng);
msg.getData().putString("desc", desc);
msg.getData().putLong("time", location.getTime());
msg.getData().putDouble("radius", location.getAccuracy());
msg.getData().putString("city", location.getCity());
msg.getData().putString("country", location.getDistrict());
msg.getData().putString("province", location.getProvince());
msg.getData().putString("provider", location.getProvider());
/**
* 更新客户端定位的手机位置信息
*/
sharePre.putPres(Constants.PREFERENCE_KEY_LATITUDE, geoLat + "");
sharePre.putPres(Constants.PREFERENCE_KEY_LONGITUDE, geoLng
+ "");
} else {
sb.append("定位失败,默认位置为郑州,请自行调整");
}
// writeFile.writeToFileAdditional(sb.toString());
msg.what = 5;
msg.obj = sb.toString();
if (handler != null) {
handler.sendMessage(msg);
}
} else {
System.out.println("位置是空的");
// writeFile.writeToFileAdditional("位置是空的");
}
}
@Override
protected void onResume() {
super.onResume();
Log.e("WearthActivity:", "onResume");
}
@Override
protected void onPause() {
super.onPause();
disableMyLocation();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 5:
province = msg.getData().getString("province");
city = msg.getData().getString("city");
if (province == null) { // 如北京,省返回空
province = city;
}
if (!TextUtils.isEmpty(city)) {
optBtn.setText(" " + (null==city?"":city) + " ");
disableMyLocation();
getWeather(false,false);
}
break;
default:
break;
}
}
};
/**
* 获取天气数据
*
* @param isGetWeatherByTID
* isGetWeatherByTID为true时表示需要通过城市的TID查询天气,否则通过省市获取天气数据
* @param showDilag
* 是否显示对话框
*/
private void getWeather(Boolean isGetWeatherByTID ,Boolean showDilag) {
if (isGetWeatherByTID) {
new GetWeatherNewTaskNew(WearthActivityNew.this, showDilag,
cityBean.getCityId(), new ReturnWeatherData() {
@Override
public void getWeatherData(WeatherBean weather) {
showUI(weather);
}
}).execute();
} else {
new GetWeatherNewTaskNew(province, city, WearthActivityNew.this,
showDilag, new ReturnWeatherData() {
@Override
public void getWeatherData(WeatherBean weather) {
showUI(weather);
}
}).execute();
}
}
/**
* 显示页面数据
* @param weather
*/
private void showUI(WeatherBean weather){
if (null != weather) {
setUIData(weather);
}
}
/**
* 根据天气数据 将数据显示到页面上
*
* @param weather
*/
private void setUIData(WeatherBean weather) {
UIUtil.showView(weatherLayout);
UIUtil.hideView(load_Layout);
String oneCelsius = weather.getOneCelsius();
if (oneCelsius.contains("/")) {
weatherCelsiusTV.setText(oneCelsius.split("/")[0]);
weatherTempterTV.setText(" /" + oneCelsius.split("/")[1]);
} else {
weatherCelsiusTV.setText(oneCelsius);
}
optBtn.setText(" " + (null==city?"":city) + " ");
weatherTV.setText(weather.getOneCloud());
weatherCloudTV.setText(weather.getOneWind());
/**
* 取出后四天的天气数据
*/
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String week = sdf.format(date);
weatherDateTV.setText(week);
calendar.add(Calendar.DAY_OF_YEAR, 1);
date = calendar.getTime();
oneDateTV.setText(sdf.format(date));
oneWeatherTV.setText(weather.getTwoCloud() + " "
+ weather.getTwoCelsius());
calendar.add(Calendar.DAY_OF_YEAR, 1);
date = calendar.getTime();
twoDateTV.setText(sdf.format(date));
twoWeatherTV.setText(weather.getThreeCloud() + " "
+ weather.getThreeCelsius());
calendar.add(Calendar.DAY_OF_YEAR, 1);
date = calendar.getTime();
threeDateTV.setText(sdf.format(date));
threeWeatherTV.setText(weather.getFourCloud() + " "
+ weather.getFourCelsius());
calendar.add(Calendar.DAY_OF_YEAR, 1);
date = calendar.getTime();
fourDateTV.setText(sdf.format(date));
fourWeatherTV.setText(weather.getFiveCloud() + " "
+ weather.getFiveCelsius());
weatherPic(weather.getWheatherId());
}
// 天气图片
private void weatherPic(String weatherPic) {
Bitmap bm = getBitmap("http://www.njxxw.com.cn/cityphoto/2/"
+ weatherPic + ".jpg");
if (null != bm) {
BitmapDrawable drawable = new BitmapDrawable(
WearthActivityNew.this.getResources(), bm);
if (null != drawable) {
middlelayout.setBackgroundDrawable(drawable);
} else {
middlelayout.setBackgroundDrawable(getResources().getDrawable(
R.drawable.tqyb));
}
} else {
middlelayout.setBackgroundDrawable(getResources().getDrawable(
R.drawable.tqyb));
}
}
// 根据图片的url得到要显示的图片
private Bitmap getBitmap(String url) {
Bitmap bm = null;// 生成了一张bmp图像
try {
// URL iconurl = new URL("http://m.weather.com.cn/img/b" + url
// + ".gif");
URL iconurl = new URL(url);
URLConnection conn = iconurl.openConnection();
conn.connect();
// 获得图像的字符流
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8192);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();// 关闭流
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
private void setUpBaiduLocation() {
/**
* 接收百度定位类传来的参数
*/
LocationClient client = new LocationClient(this);
client.request(new ILocationListener() {
@Override
public void onReceiveLocInfo(int locType, LocationInfo loc) {
handler.sendEmptyMessage(2);
Message msg = new Message();
if (loc!= null) {
Double geoLat = loc.getLat();
Double geoLng = loc.getLon();
StringBuffer sb = new StringBuffer();
if (loc.getSuccess() == 1) {
sb.append("定位方式:").append(loc.getPosour());
sb.append("\n定位时间:").append(
AMapUtil.convertToTime(loc.getTime()));
sb.append("\n位置描述:").append(loc.getContent());
sb.append("\n省:").append(loc.getProvince());
sb.append("\n市:").append(loc.getCity());
sb.append("\n区(县):").append(loc.getCountry());
sb.append("\n详细经纬度" + geoLat + "-------" + geoLng);
msg.getData().putDouble("latitude", geoLat);
msg.getData().putDouble("longitude", geoLng);
msg.getData().putLong("time", loc.getTime());
msg.getData().putString("city", loc.getCity());
msg.getData().putString("country", loc.getCountry());
msg.getData().putString("province", loc.getProvince());
msg.getData().putInt("provider", loc.getPosour());
/**
* 更新客户端定位的手机位置信息
*/
sharePre.putPres(Constants.PREFERENCE_KEY_LATITUDE, geoLat + "");
sharePre.putPres(Constants.PREFERENCE_KEY_LONGITUDE, geoLng
+ "");
} else {
sb.append("定位失败,默认位置为郑州,请自行调整");
}
// writeFile.writeToFileAdditional(sb.toString());
msg.what = 5;
msg.obj = sb.toString();
if (handler != null) {
handler.sendMessage(msg);
}
} else {
System.out.println("位置是空的");
// writeFile.writeToFileAdditional("位置是空的");
}
}
});
}
}