package com.jqyd.android.module.lbs; import java.text.SimpleDateFormat; import java.util.Date; import com.jqyd.android.module.lbs.Interface.ILocationListener; import com.jqyd.android.module.lbs.bean.GeocodeInfo; import com.jqyd.android.module.lbs.bean.LocationInfo; import com.jqyd.android.module.lbs.util.CheckState_interface; import com.jqyd.android.module.lbs.util.JqydDateUtil; import com.jqyd.android.module.lbs.util.Optsharepre_interface; import com.jqyd.android.module.lbs.util.WriteFile; import android.app.Application; import android.content.Context; import android.content.SharedPreferences; import android.telephony.TelephonyManager; import android.telephony.cdma.CdmaCellLocation; import android.telephony.gsm.GsmCellLocation; import android.util.Log; import android.widget.Toast; public class LocationUtils { Context context; public static final String TAG = "LOCUTILS"; private String log = "";// 记录日志 private Application myApp; private static final String SHARE_FILES = "LOCATION_FILES"; private static SharedPreferences loationSharedFile; private WriteFile wf = new WriteFile(TAG + JqydDateUtil.getDateDayOne(new Date())); private TelephonyManager tm; private Optsharepre_interface share; public LocationUtils(Context context) { super(); this.context = context; myApp = (Application) context.getApplicationContext(); tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); loationSharedFile = context.getSharedPreferences(SHARE_FILES, 0); share = new Optsharepre_interface(context); } /** * * Description: 验证经纬度是否合法 Title: validateLonLat * * @param longitude * 经度 * @param latitude * 纬度 * @return boolean true数据没问题false数据为空 */ public boolean validateLonLat(String longitude, String latitude) { boolean b = true; if (longitude.equals("0") || latitude.equals("0") || longitude.equals("0.0") || latitude.equals("0.0") || "".equals(longitude) || "".equals(latitude) || longitude == null || latitude == null) { b = false; } return b; } /** * * Description: 获取百度的位置信息 Title: getBaiduLocationData * * @param loc * 位置对象 * @param baidu * 百度对象 * @return int 0失败 1成功 */ public LocationInfo getBaiduLocationData(ILocationListener lintener, LocationInfo loc, Baidu_location baidu) { int result = 0;// 默认失败 Log.d("location", "启动百度监听器,开始搜索位置……" + new SimpleDateFormat("yyyy-mm-dd HH:mm:ss") .format(new Date())); wf.writeToFile("启动百度监听器,开始搜索位置……"); if (baidu != null && loc==null) { baidu.mLocationClient.start(); baidu.mLocationClient.requestLocation(); wf.writeToFile("baidu start……"); } else { wf.writeToFile("baidu is null……"); } String locInfo = ""; // 1、获取百度位置 for (int i = 0; i < 10; i++) { Log.d("location", "---------" + baidu); wf.writeToFile("baidu is start……" + baidu.mLocationClient.isStarted()); try { if (baidu != null ) { Log.d("location", "百度监听器已经注册并启动成功"); wf.writeToFile("百度监听器已经注册并启动成功"); if (loc != null) {// 请求百度获取位置成功 String bdTimeTemp = ""; int CellIdTemp = 0; try { bdTimeTemp = loationSharedFile.getString( "bdTimeTemp", "-1"); CellIdTemp = loationSharedFile.getInt("cellId", 0); } catch (NumberFormatException e) { e.printStackTrace(); Log.i(TAG, "获取历史时间出现异常"); } int currCellId = getCellId();// 当前网络CellId // 只有161为网络交互结果成功,其他为失败,暂时不调用百度的gps信息,且获取的位置为最新位置 // 记录日志 log = "《获取位置--百度定位》*******************百度返回代码:" + loc.getErrorcode() + ",当前网络CellId:" + currCellId + ",上次获取网络CellId:" + currCellId + ",当前百度时间:" + new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(loc.getTime()) + ",上次百度时间:" + bdTimeTemp + ",获取位置信息:经度:" + loc.getLon() + ",纬度:" + loc.getLat()+ ",省:" + loc.getProvince()+ ",市:" + loc.getCity()+ ",县:" + loc.getCountry()+ ",地址:" +loc.getContent()+ ",type:" + loc.getPosour(); wf.writeToFile(log); if ((loc.getErrorcode()==161||validateLonLat(loc.getLat()+"",loc.getLon()+"")) && ((loc.getTime()==Long.parseLong(bdTimeTemp) && currCellId == CellIdTemp) || loc.getTime()!= Long.parseLong(bdTimeTemp))) { // 记录百度时间变量参数 SharedPreferences.Editor editor = loationSharedFile.edit(); editor.putString("bdTimeTemp", loc.getTime()+""); editor.putInt("cellId", currCellId); editor.commit(); Log.i(TAG, "locinfo " + locInfo); } else { Log.i(TAG, "************获取位置失败**************"); // 记录百度定位返回的错误代码 } } else { Log.i(TAG, "************获取位置失败**************"); // 记录百度定位返回的错误代码 } } } catch (Exception e1) { e1.printStackTrace(); wf.writeToFile("请求百度获取位置出现异常"+e1.getMessage()); } if (loc!=null) {// 获取到位置跳出循环 Log.i(TAG, "获取到百度位置,跳出循环"); loc.setSuccess(1); break; } else {// 获取位置失败,休息1s中后继续判断 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } if(lintener!=null)lintener.onReceiveLocInfo(1, loc); if(baidu!=null)baidu.stopListener(); return loc; } /** * 获取当前网络CellId * * @return */ public int getCellId() { int cid = 0; int type = tm.getNetworkType(); /** * 当前使用的网络类型: * 例如:NETWORK_TYPE_UNKNOWN 网络类型未知 0 NETWORK_TYPE_GPRS GPRS网络 1 NETWORK_TYPE_EDGE EDGE网络 2 NETWORK_TYPE_UMTS UMTS网络 3 NETWORK_TYPE_HSDPA HSDPA网络 8 NETWORK_TYPE_HSUPA HSUPA网络 9 NETWORK_TYPE_HSPA HSPA网络 10 NETWORK_TYPE_CDMA CDMA网络,IS95A 或 IS95B. 4 NETWORK_TYPE_EVDO_0 EVDO网络, revision 0. 5 NETWORK_TYPE_EVDO_A EVDO网络, revision A. 6 NETWORK_TYPE_1xRTT 1xRTT网络 7 */ // 在中国,联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO // G网 /** Network type is unknown NETWORK_TYPE_UNKNOWN = 0; Current network is GPRS NETWORK_TYPE_GPRS = 1; Current network is EDGE NETWORK_TYPE_EDGE = 2; Current network is UMTS NETWORK_TYPE_UMTS = 3; Current network is CDMA: Either IS95A or IS95B NETWORK_TYPE_CDMA = 4; Current network is EVDO revision 0 NETWORK_TYPE_EVDO_0 = 5; Current network is EVDO revision A NETWORK_TYPE_EVDO_A = 6; Current network is 1xRTT NETWORK_TYPE_1xRTT = 7; Current network is HSDPA NETWORK_TYPE_HSDPA = 8; Current network is HSUPA NETWORK_TYPE_HSUPA = 9; Current network is HSPA NETWORK_TYPE_HSPA = 10; Current network is iDen NETWORK_TYPE_IDEN = 11;综合数字增强网络(简称iDEN),是摩托罗拉发明的一种无线通信技术 Current network is EVDO revision B NETWORK_TYPE_EVDO_B = 12; Current network is LTE NETWORK_TYPE_LTE = 13; Current network is eHRPD NETWORK_TYPE_EHRPD = 14; Current network is HSPA+ TelephonyManager.NETWORK_TYPE_HSPAP = 15; */ try { if (type == TelephonyManager.NETWORK_TYPE_UMTS || type == TelephonyManager.NETWORK_TYPE_HSDPA || type == TelephonyManager.NETWORK_TYPE_EDGE || type == TelephonyManager.NETWORK_TYPE_GPRS || type == TelephonyManager.NETWORK_TYPE_HSPA ||type==TelephonyManager.NETWORK_TYPE_LTE//3.0版本一上 || type == TelephonyManager.NETWORK_TYPE_HSUPA) { GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); cid = gcl.getCid();// 小区编号 // C网 } else if (type == TelephonyManager.NETWORK_TYPE_CDMA || type == TelephonyManager.NETWORK_TYPE_EVDO_0 || type == TelephonyManager.NETWORK_TYPE_EVDO_A ||type==TelephonyManager.NETWORK_TYPE_1xRTT ||type==TelephonyManager.NETWORK_TYPE_EHRPD //3.0版本一上 ||type==TelephonyManager.NETWORK_TYPE_EVDO_B //3.0版本一上 ) { // 参考地址:http://topic.csdn.net/u/20110110/23/c2f6524a-8746-4c1b-914e-57c7c9cced02.html CdmaCellLocation cdl = (CdmaCellLocation) tm.getCellLocation(); cid = cdl.getBaseStationId(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return cid; } /** * * Description: 获取GPS的位置信息 Title: getGPSLocationData * * @param loc * 位置对象 * @return int 0失败1成功-1GPS未打开 */ public LocationInfo getGPSLocationData(LocationInfo loc,ILocationListener lintener) { int result = -1;// 默认GPS没有开 String locInfo = ""; // 请求卫星 // 判断是否开启GPS int gps_flag = new CheckState_interface(context).checkGpState(); if (gps_flag == 0) {// GPS设备未打开 // 记录日志 log = "GPS模块未打开,获取位置失败!"; loc.setGpsStatus(0); share.editPres("gpsStatus", loc.getGpsStatus()+""); loc.setWlanStatus(new CheckState_interface(context).checkWiFi()); share.editPres("wlanStatus", loc.getWlanStatus()+""); loc.setMobileNetworkStatus(new CheckState_interface(context).checkNet()); share.editPres("mobileNetworkStatus", loc.getMobileNetworkStatus()+""); wf.writeToFile(log); loc.setSuccess(0); lintener.onReceiveLocInfo(2, loc); return loc; } else {// GPS设备已打开 wf.writeToFile("GPS设备已打开,启动GPS线程"); GpsThread gpsThread = new GpsThread(myApp, loationSharedFile); gpsThread.start(); // --------------------------------- for (int k = 0; k < 3; k++) { if (!locInfo.equals("") && !locInfo.equals("1")) { break; } else { try { Thread.sleep(1000);// 每次休眠10s,共60s } catch (InterruptedException e) { e.printStackTrace(); } } long gpsTimeTemp = 0; try { gpsTimeTemp = loationSharedFile.getLong("gpsTimeTemp", -1); } catch (NumberFormatException e) { e.printStackTrace(); Log.i(TAG, "获取历史时间出现异常"); } int activeInt = loationSharedFile.getInt("active", -1); String lat = loationSharedFile.getString("lat", "-1"); String lon = loationSharedFile.getString("lon", "-1"); long time = loationSharedFile.getLong("time", 0); log = "《获取位置--卫星定位》*******************" + "当前时间:" + time + ",上次时间:" + gpsTimeTemp + ",获取位置信息:经度:" + lon + ",纬度:" + lat; wf.writeToFile("GPSlog:" + time + "," + lon + "," + lat + ",历史时间:" + gpsTimeTemp + " isActive:" + activeInt + "\n"); Log.i(TAG, time + "," + lon + "," + lat + ",历史时间:" + gpsTimeTemp); if ((activeInt == 1) && (time != gpsTimeTemp)) {// 卫星获取成功,且当前卫星时间和上次卫星时间不相同 // locInfo = "0#" + lon + "#" + lat + "#" + time; if (validateLonLat(lon, lat)) { try { loc.setLon(Double.valueOf(lon).doubleValue()); share.editPres("lon", loc.getLon()+""); loc.setLat(Double.valueOf(lat).doubleValue()); share.editPres("lat", loc.getLat()+""); loc.setTime(time); share.editPres("time", loc.getTime()+""); loc.setRadius(0); share.editPres("radius", "0"); loc.setSuccess(1); share.editPres("success", "1"); loc.setPosour(1); share.editPres("posour", "1"); loc.setGisInfo(new GeocodeInfo(loc.getLon(), loc.getLat())); share.editPres("province", loc.getGisInfo().getProvince()); share.editPres("city", loc.getGisInfo().getCity()); share.editPres("code", loc.getGisInfo().getCode()); share.editPres("country", loc.getGisInfo().getCountry()); share.editPres("poi", loc.getGisInfo().getPoi()); share.editPres("content", loc.getGisInfo().getContent()); share.editPres("xCross", loc.getGisInfo().getCross()); share.editPres("xmlStr", loc.getGisInfo().getXmlStr()); share.editPres("str1", loc.getGisInfo().getStr1()); GeocodeInfo geo = new GeocodeInfo(loc.getLon(), loc.getLat()); loc.setGisInfo(geo); lintener.onReceiveLocInfo(2, loc); return loc; // result = 1;// 获取成功 } catch (NumberFormatException e) { e.printStackTrace(); result = 0; } } // 记录gps的时间变量参数 SharedPreferences.Editor editor = loationSharedFile.edit(); editor.putLong("gpsTimeTemp", time); editor.commit(); } else { locInfo = "1";// 获取位置失败 } Log.i(TAG, "GPSlog:" + log); wf.writeToFile(log); } gpsThread.stopGspListener(); lintener.onReceiveLocInfo(2,loc); } return loc; } public boolean updateDwMode(int dwMode) { SharedPreferences.Editor editor = loationSharedFile.edit(); editor.putInt("DWMODE", dwMode); return editor.commit(); } /** * @param locType 1--百度优先 2--gps优先 3-- 仅gps 4-- 仅百度 * @param lintener */ public void getLocData(int locType,final ILocationListener lintener){ if(!(locType==1||locType==2||locType==3||locType==4)){ throw new RuntimeException("定位类型只能传1,2,3,4,不能传其他值。"); } switch (locType) { case 1:// 百度优先 new Baidu_location(context, new ILocationListener() { @Override public void onReceiveLocInfo(int result, LocationInfo bean) { if (bean.getSuccess() == 1) { lintener.onReceiveLocInfo(1, bean); } else { getGPSLocationData(bean,new ILocationListener() { @Override public void onReceiveLocInfo(int i,LocationInfo bean) { if(bean.getSuccess()==1){ lintener.onReceiveLocInfo(2, bean); }else{ lintener.onReceiveLocInfo(2, bean); } } }); } } }); break; case 2:// gps优先 LocationInfo bean = new LocationInfo(); getGPSLocationData(bean,new ILocationListener() { @Override public void onReceiveLocInfo(int i,LocationInfo bean) { if (bean.getSuccess() == 1) { lintener.onReceiveLocInfo(2, bean); } else { new Baidu_location(context, new ILocationListener() { @Override public void onReceiveLocInfo(int result, LocationInfo bean) { if (bean.getSuccess() == 1) { lintener.onReceiveLocInfo(1, bean); } else { lintener.onReceiveLocInfo(1, bean); } } }); } } }); break; case 3:// 仅gps if (new CheckState_interface(context).checkGpState() == 0) { Toast.makeText(context, "请打开GPS", Toast.LENGTH_SHORT) .show(); return; } getGPSLocationData(new LocationInfo(),new ILocationListener() { @Override public void onReceiveLocInfo(int i,LocationInfo bean) { if(bean.getSuccess()==1){ lintener.onReceiveLocInfo(2, bean); }else{ lintener.onReceiveLocInfo(2, bean); } } }); break; case 4:// 仅百度 new Baidu_location(context, new ILocationListener() { @Override public void onReceiveLocInfo(int result, LocationInfo bean) { if (bean.getSuccess() == 1) { lintener.onReceiveLocInfo(1, bean); } else { lintener.onReceiveLocInfo(1, bean); } } }); break; default: break; } } }