基站定位代码封装

在真机上测试:
要点一: 一定要插手机卡 ---得到cid通过移动
要点二: 一定要能上网 ----得到经纬度---通过google

public class BaseStationManager {
Context c;
private TelephonyManager phoneService;
String json = "aaa";
private static volatile BaseStationManager bsManager ;
private BaseStationManager(Context c){
this.c = c;
}

public static BaseStationManager getInstance(Context c){
if(bsManager == null){
synchronized(BaseStationManager.class){
if(bsManager == null){
bsManager = new BaseStationManager(c);
}
}
}//if
return bsManager;
}

//=======================================================================================
public void init(){

phoneService = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
update();
}

public String getLatitude(){
String latitude = "null";
Pattern p = Pattern.compile("latitude\":[^,]+");
Matcher m = p.matcher(json);
if(m.find()){
latitude = m.group().split(":")[1];
}

return latitude;
}

public String getLongitude(){
String latitude = "null";
Pattern p = Pattern.compile("longitude\":[^,]+");
Matcher m = p.matcher(json);
if(m.find()){
latitude = m.group().split(":")[1];
}

return latitude;
}





public void update(){
GsmCellLocation gcl = (GsmCellLocation) phoneService.getCellLocation();

int cid = gcl.getCid();//CID for GSM
int lac = gcl.getLac();

int mcc = Integer.valueOf(phoneService.getNetworkOperator().substring(0,3));

int mnc = Integer.valueOf(phoneService.getNetworkOperator().substring(3,5));
json = "bbb";
try {
//组装JSON查询字符串
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", cid);
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);

//=============json封装成StringEntity,StringEntity让入post, post让client执行

// 创建连接,发送请求并接受回应
DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(
"http://www.google.com/loc/json");

StringEntity se = new StringEntity(holder.toString());

post.setEntity(se);
HttpResponse resp = client.execute(post);

HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();

String l = null;
while ((l=br.readLine()) != null) {
sb.append(l);
}
json = sb.toString();
}catch(Exception e){

}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值