private void addCellSpecFromCellLocation( Collection<CellSpec> cellSpecs, int mcc, int mnc, CellLocation cellLocation) { if (cellLocation instanceof GsmCellLocation) { GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation; int cid = gsmCellLocation.getCid(); int lac = gsmCellLocation.getLac(); int psc = gsmCellLocation.getPsc(); if (psc == -1) { cellSpecs.add(new CellSpec(Radio.GSM, mcc, mnc, lac, cid)); } else { CellSpec cellSpec = new CellSpec(Radio.UMTS, mcc, mnc, lac, cid); cellSpec.setPsc(psc); cellSpecs.add(cellSpec); } } else if (cellLocation instanceof CdmaCellLocation) { CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation; int sid = cdmaCellLocation.getSystemId(); // as mnc int nid = cdmaCellLocation.getNetworkId(); // as lac int bid = cdmaCellLocation.getBaseStationId(); // as cid cellSpecs.add(new CellSpec(Radio.CDMA, mcc, sid, nid, bid)); } else { if (MainService.DEBUG) { Log.d(TAG, "Not connected to network or using LTE, which is not supported for SDK <= 16"); } } }
public void getLocationByBase() { telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); try { int type = telephonyManager.getNetworkType(); int lac = 0; int ci = 0; if (type == TelephonyManager.PHONE_TYPE_GSM) { Log.e("LocatteMySeleftService", "GSM"); GsmCellLocation location = (GsmCellLocation) telephonyManager.getCellLocation(); lac = location.getLac(); ci = location.getCid(); int mcc = Integer.valueOf(telephonyManager.getNetworkOperator().substring(0, 3)); int mnc = Integer.valueOf(telephonyManager.getNetworkOperator().substring(3, 5)); JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("request_address", true); JSONArray array = new JSONArray(); JSONObject data = new JSONObject(); data.put("cell_id", ci); data.put("location_area_code", lac); data.put("mobile_country_code", mcc); data.put("mobile_network_code", mnc); array.put(data); holder.put("cell_towers", array); DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.google.com/loc/json"); StringEntity se = new StringEntity(holder.toString()); post.setEntity(se); org.apache.http.HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); StringBuffer sb = new StringBuffer(); String result = br.readLine(); while (result != null) { sb.append(result); result = br.readLine(); } JSONObject jsonObject = new JSONObject(sb.toString()); JSONObject jsonObject1 = new JSONObject(jsonObject.getString("location")); getAddress(jsonObject1.getString("latitude"), jsonObject1.getString("longitude")); } else if (type == TelephonyManager.PHONE_TYPE_CDMA || type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_1xRTT) { Log.e("LocatteMySeleftService", "CDMA"); CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) telephonyManager.getCellLocation(); lac = cdmaCellLocation.getNetworkId(); ci = cdmaCellLocation.getBaseStationId(); int sid = cdmaCellLocation.getSystemId(); int mcc = Integer.valueOf(telephonyManager.getNetworkOperator().substring(0, 3)); JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("request_address", true); holder.put("radio_type", "cdma"); JSONArray array = new JSONArray(); JSONObject data = new JSONObject(); data.put("cell_id", ci); data.put("location_area_code", lac); data.put("mobile_country_code", mcc); data.put("mobile_network_code", sid); data.put("address_language", "zh_CN"); data.put("age", 0); array.put(data); holder.put("cell_towers", array); Log.e("LocateMySelftService", holder.toString()); DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.google.com/loc/json"); StringEntity se = new StringEntity(holder.toString()); post.setEntity(se); org.apache.http.HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); StringBuffer sb = new StringBuffer(); String result = br.readLine(); while (result != null) { sb.append(result); result = br.readLine(); } Log.e("LocateMySelftService", sb.toString()); JSONObject jsonObject = new JSONObject(sb.toString()); JSONObject jsonObject1 = new JSONObject(jsonObject.getString("location")); getAddress(jsonObject1.getString("latitude"), jsonObject1.getString("longitude")); } else { handler.sendEmptyMessage(MESSAGE_FAILED_USING_BASE); } } catch (Exception e) { handler.sendEmptyMessage(MESSAGE_FAILED_USING_BASE); } }