[置顶] 通过SIM卡获取GPS,android基站定位原理
16lz
2021-01-23
TelephonyManager telManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);GsmCellLocation glc=(GsmCellLocation) telManager.getCellLocation();int cid=glc.getCid();///gsm cell id, -1 if unknown, 0xffff max legal value 基站ID号int lac=glc.getLac();//写入区域代码String strOperator=telManager.getNetworkOperator();int mcc=Integer.valueOf(strOperator.substring(0, 3));//写入当前城市代码int mnc=Integer.valueOf(strOperator.substring(3, 5));//写入网络代码String getNumber="";getNumber+=("cid:"+cid+"\n");getNumber+=("cid:"+lac+"\n");getNumber+=("cid:"+mcc+"\n");getNumber+=("cid:"+mnc+"\n");try {JSONObject jObject=new JSONObject();jObject.put("version", "1.1.0");jObject.put("host", "maps.google.com");jObject.put("request_address", true);if(mcc==460)jObject.put("address_language","zh_CN");elsejObject.put("address_language", "en_US");JSONArray jArray=new JSONArray();JSONObject jData=new JSONObject();jData.put("cell_id", cid);jData.put("location_area_code", lac);jData.put("mobile_country_code", mcc);jData.put("mobile_network_code", mnc);//jArray.put(jData);jObject.put("cell_towers",jArray);DefaultHttpClient client=new DefaultHttpClient();HttpPost post=new HttpPost("http://www.google.com/loc/json");StringEntity se=new StringEntity(jObject.toString());post.setEntity(se);HttpResponse resp=client.execute(post);BufferedReader br=null;if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));StringBuffer sb=new StringBuffer();String result=br.readLine();while(result!=null){sb.append(getNumber);sb.append(result);result=br.readLine();}String s=sb.toString();s=s.substring(s.indexOf("{"));btn.setText(s);JSONObject jo=new JSONObject(s);String values="";JSONObject arr= jo.getJSONObject("location");String lat= arr.get("latitude").toString();String lon=arr.getString("longitude").toString();Toast.makeText(getApplicationContext(), "经度:"+lon+" 纬度:"+lat,1).show();}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}最后还要加入网络访问权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
更多相关文章
- ANDROID图片压缩代码
- Android 如何在Java代码中手动设置控件的marginleft
- android典型代码系列(十)------获取一个应用程序的权限信息(反射
- android播放音乐文件代码
- 分享一段Android基于https协议POST数据的代码