package com.jqmobile.core.android.lbs.base; //import java.text.SimpleDateFormat; //import java.util.Date; //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 com.jqyd.model.LocationModule; //import com.jqyd.pub.date.JqydDateUtil; //import com.jqyd.pub.log.WriteFile; //import com.jqyd.pub.time.GetFromServer; //import com.jqyd.shareInterface.Baidu_location; //import com.jqyd.shareInterface.CheckState_interface; public class LocationUtils { // Context context; // private 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; // // /** // * 获取当前基站的cid及相关信息 // */ // // 解释: // // IMSI是国际移动用户识别码的简称(International Mobile Subscriber Identity) // // IMSI共有15位,其结构如下: // // MCC+MNC+MIN // // MCC:Mobile Country Code,移动国家码,共3位,中国为460; // // MNC:Mobile NetworkCode,移动网络码,共2位 // // 在中国,移动的代码为电00和02,联通的代码为01,电信的代码为03 // // public LocationModule takeCellInfos() { // LocationModule loc = new LocationModule(); // int cid = 0; // int lac = 0; // int type = tm.getNetworkType(); // // 在中国,联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO // // G网 // wf.writeToFile("当前网络类型:" + type); // 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_HSUPA) { // GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); // try { // cid = gcl.getCid();// 小区编号 // lac = gcl.getLac();// 位置区域码 // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // wf.writeToFile("G网获取小区编号或位置区域码出现错误!"); // } // Log.i(TAG, "------------G网cid--------------:" + cid); // // 信号强度 // // C网 // } else if (type == TelephonyManager.NETWORK_TYPE_CDMA // || type == TelephonyManager.NETWORK_TYPE_EVDO_0 // || type == TelephonyManager.NETWORK_TYPE_EVDO_A) { // // 参考地址:http://topic.csdn.net/u/20110110/23/c2f6524a-8746-4c1b-914e-57c7c9cced02.html // CdmaCellLocation cdl = (CdmaCellLocation) tm.getCellLocation(); // try { // cid = cdl.getBaseStationId(); // lac = cdl.getNetworkId(); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // wf.writeToFile("C网获取小区编号或位置区域码出现错误!"); // } // // 信号强度 // Log.i(TAG, "------------------C网cid-------------:" + cid); // } // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // wf.writeToFile("------------读取小区编号时出现异常-------------"); // } // Log.i(TAG, "本机网络类型:" + type); // loc.setCell_id(cid + "");// 小区编号 // loc.setLac(lac + "");// 位置区域码 // loc.setCcode(tm.getSubscriberId().substring(0, 3));// 移动国家码 // loc.setNcode(tm.getSubscriberId().substring(3, 5));// 移动网络码 // String operator = tm.getSimOperator(); // if (operator != null) { // if (operator.equals("46000") || operator.equals("46002")) { // // 中国移动 // operator = "3"; // } else if (operator.equals("46001")) { // // 中国联通 // operator = "1"; // } else if (operator.equals("46003")) { // // 中国电信 // operator = "2"; // } // } else { // operator = "-1"; // } // loc.setYys(operator); // return loc; // } // // 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); // // } // // /** // * // * Description: 获取小区的位置信息 Title: getCellLocationData // * // * @param loc // * 位置对象 // * @param custId // * 客户id int 0失败1成功 // */ // public int getCellLocationData(LocationModule loc, String custId) { // int result = 0;// 默认失败 // // 1、获取小区位置 // int cellId = -1; // int lac = 0; // try { // cellId = Integer.parseInt(loc.getCell_id()); // lac = Integer.parseInt(loc.getLac()); // } catch (NumberFormatException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // // // shareMethod.recordLog("当前网络基站编号:" + cellId + "客户编号:" + custId); // String locInfo = "";// share_loc.getCellInfo(cellId, custId,lac); // if (!locInfo.equals("")) {// 小区位置获取成功 // Log.i(TAG, "******************通过小区获取位置信息******************"); // String longitude = locInfo.split("#")[1]; // String latitude = locInfo.split("#")[2]; // if (validateLonLat(longitude, latitude)) { // try { // loc.setLon(Double.valueOf(longitude).doubleValue()); // loc.setLat(Double.valueOf(latitude).doubleValue()); // loc.setRadius(locInfo.split("#")[4]); // result = 1; // } catch (NumberFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // result = 0; // } // // } // } // return result; // } // // /** // * // * 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 int getBaiduLocationData(LocationModule 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) { // 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 && baidu.mLocationClient.isStarted()) { // baidu.mLocationClient.requestLocation();// 定位 // Log.d("location", "百度监听器已经注册并启动成功"); // wf.writeToFile("百度监听器已经注册并启动成功"); // if (baidu.getSb() != null) {// 请求百度获取位置成功 // String bdTimeTemp = ""; // int CellId = 0; // try { // bdTimeTemp = loationSharedFile.getString( // "bdTimeTemp", "-1"); // CellId = loationSharedFile.getInt("cellId", 0); // } catch (NumberFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // Log.i(TAG, "获取历史时间出现异常"); // } // // Log.i(TAG, "请求Baidu,获得结果:" + baidu.getSb()); // String ret = baidu.getSb().toString(); // wf.writeToFile("请求Baidu,获得结果:" + ret); // String[] strs = ret.split(";"); // String baiduCode = strs[3]; // int cellId = getCellId();// 当前网络CellId // Log.i(TAG, "*****************百度返回代码-----------------" // + baiduCode + " " + cellId + "==" + CellId // + " " + bdTimeTemp); // // 只有161为网络交互结果成功,其他为失败,暂时不调用百度的gps信息,且获取的位置为最新位置 // // // 记录日志 // log = "《获取位置--百度定位》*******************百度返回代码:" // + baiduCode + ",当前网络CellId:" + cellId // + ",上次获取网络CellId:" + cellId + ",当前百度时间:" // + strs[1] + ",上次百度时间:" + bdTimeTemp // + ",获取位置信息:经度:" + strs[7] + ",纬度:" + strs[5]; // wf.writeToFile(log); // if (baiduCode.equals("161") // && ((strs[1].equals(bdTimeTemp) && cellId == CellId) || !strs[1] // .equals(bdTimeTemp))) { // locInfo = "3#" + strs[7] + "#" + strs[5] + "#" // + strs[1] + "#" + strs[9] + "#" + "-1"; // // 记录百度时间变量参数 // SharedPreferences.Editor editor = loationSharedFile // .edit(); // editor.putString("bdTimeTemp", strs[1]); // editor.putInt("cellId", cellId); // editor.commit(); // // Log.i(TAG, "locinfo " + locInfo); // } else { // Log.i(TAG, "************获取位置失败**************"); // // 记录百度定位返回的错误代码 // // share_obj.editPres("errCode", baiduCode); // } // } else { // Log.i(TAG, "************获取位置失败**************"); // // 记录百度定位返回的错误代码 // // share_obj.editPres("errCode", "-1"); // } // // } // } catch (Exception e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // wf.writeToFile("请求百度获取位置出现异常"); // } // if (!locInfo.equals("")) {// 获取到位置跳出循环 // Log.i(TAG, "获取到百度位置,跳出循环"); // wf.writeToFile("获取到百度位置,跳出循环" + locInfo); // break; // } else {// 获取位置失败,休息1s中后继续判断 // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // } // if (!locInfo.equals("")) {// 百度获取位置成功 // Log.i(TAG, "******************通过百度获取位置信息******************"); // String longitude = locInfo.split("#")[1]; // String latitude = locInfo.split("#")[2]; // if (validateLonLat(longitude, latitude)) { // try { // loc.setLon(Double.valueOf(longitude).doubleValue()); // loc.setLat(Double.valueOf(latitude).doubleValue()); // loc.setRadius(locInfo.split("#")[4]); // loc.setTime(0);// 百度返回的时间不准确,故采用服务器端时间 // result = 1; // } catch (NumberFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // result = 0; // } // } // // } else { // wf.writeToFile("百度定位,获取失败"); // } // // return result; // } // // /** // * 获取当前网络CellId // * // * @return // */ // public int getCellId() { // int cid = 0; // int type = tm.getNetworkType(); // // 在中国,联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO // // G网 // 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_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) { // // 参考地址: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 int getGPSLocationData(LocationModule loc) { // // int result = -1;// 默认GPS没有开 // String locInfo = ""; // // 请求卫星 // // 判断是否开启GPS // int gps_flag = new CheckState_interface(context).checkGpState(); // if (gps_flag == 0) {// GPS设备未打开 // /* // * Toast.makeText(context, "GPS模块不可用,请打开GPS功能!", Toast.LENGTH_LONG) // * .show(); // */ // // 记录日志 // log = "GPS模块未打开,获取位置失败!"; // wf.writeToFile(log); // return result; // } else {// GPS设备已打开 // wf.writeToFile("GPS设备已打开,启动GPS线程"); // GpsThread gpsThread = new GpsThread(myApp, loationSharedFile); // gpsThread.start(); // // --------------------------------- // for (int k = 0; k < 6; k++) { // // if (!locInfo.equals("") && !locInfo.equals("1")) { // break; // } else { // try { // Thread.sleep(10000);// 每次休眠10s,共60s // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // // long gpsTimeTemp = 0; // try { // gpsTimeTemp = loationSharedFile.getLong("gpsTimeTemp", -1); // } catch (NumberFormatException e) { // // TODO Auto-generated catch block // 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; // // 记录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(); // // } // if (!locInfo.equals("") && !locInfo.equals("1")) {// 卫星获取位置成功 // Log.i(TAG, "******************通过卫星获取位置信息******************"); // wf.writeToFile("get location from GPS " + locInfo); // String longitude = locInfo.split("#")[1]; // String latitude = locInfo.split("#")[2]; // if (validateLonLat(longitude, latitude)) { // try { // loc.setLon(Double.valueOf(longitude).doubleValue()); // loc.setLat(Double.valueOf(latitude).doubleValue()); // loc.setTime(Long.parseLong(locInfo.split("#")[3])); // loc.setRadius("0"); // result = 1;// 获取成功 // } catch (NumberFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // result = 0; // } // // } // } else { // result = 0;// 获取失败 // } // return result; // } // // public boolean updateDwMode(int dwMode) { // SharedPreferences.Editor editor = loationSharedFile.edit(); // editor.putInt("DWMODE", dwMode); // return editor.commit(); // } // // public boolean getLocation(LocationModule loc, String tag, String cusId, // Baidu_location baidu) { // int dwMode = loationSharedFile.getInt("DWMODE", 1); // // if (tag.equals("onlyGps")) { // dwMode = 2; // } // wf.writeToFile("getLocation dwMode:" + dwMode + "; tag:" + tag); // int dw_type = -1;// 0小区1卫星3百度-1定位失败 // long serverTime = GetFromServer.getServerTime(); // switch (dwMode) { // case 1: // // 网络优先 // wf.writeToFile("网络优先定位模式"); // int getLoc = 0; // getLoc = getCellLocationData(loc, cusId); // if (getLoc == 1) { // loc.setDw_type(0); // loc.setState(4);// 小区定位成功 // loc.setTime(serverTime); // return true; // } else { // getLoc = getBaiduLocationData(loc, baidu); // if (getLoc == 1) { // loc.setDw_type(3); // loc.setState(2);// 百度定位成功 // loc.setTime(serverTime); // return true; // } else { // getLoc = getGPSLocationData(loc); // if (getLoc == 1) { // loc.setDw_type(1); // loc.setState(1);// GPS定位成功 // loc.setTime(serverTime); // return true; // } else { // loc.setDw_type(-1); // loc.setState(getLoc);// 0:GPS获取失败;-1:GPS未开启 // loc.setTime(serverTime); // return false; // } // } // } // case 2: // // 仅用GPS // // wf.writeToFile("仅用GPS定位模式"); // int hasGPSLoc = getGPSLocationData(loc); // if (hasGPSLoc == 1) { // loc.setDw_type(1); // loc.setState(1);// GPS定位成功 // loc.setTime(serverTime); // return true; // } else { // loc.setDw_type(-1); // loc.setState(hasGPSLoc);// 0:GPS获取失败;-1:GPS未开启 // loc.setTime(serverTime); // return false; // } // case 3: // // GPS优先 // // wf.writeToFile("GPS优先定位模式"); // int hasLoc = 0; // hasLoc = getGPSLocationData(loc); // if (hasLoc == 1) { // loc.setDw_type(1); // loc.setState(1);// GPS定位成功 // loc.setTime(serverTime); // return true; // } else { // hasLoc = getCellLocationData(loc, cusId); // if (hasLoc == 1) { // loc.setDw_type(0); // loc.setState(4);// 小区定位成功 // loc.setTime(serverTime); // return true; // } else { // hasLoc = getBaiduLocationData(loc, baidu); // if (hasLoc == 1) { // loc.setDw_type(3); // loc.setState(2);// 百度定位成功 // loc.setTime(serverTime); // return true; // } else { // loc.setDw_type(-1); // loc.setState(3);// 百度定位失败 // loc.setTime(serverTime); // return false; // } // } // } // default: // return false; // } // // } // }