android gsm基站定位,android基站定位原理及实现代码

e929319472526277f6d4cb407e92f47d.jpg

;

;

;

;

;

;

;

;

;

importandroid.app.Activity;

importandroid.content.Context;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.telephony.TelephonyManager;

importandroid.telephony.gsm.GsmCellLocation;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.LinearLayout;

importandroid.widget.TextView;

publicclassTestextendsActivity{

Contextcontext=this;

LinearLayoutmainView=null;

Buttonbutton=null;

TextViewtv=null;

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

this.setTitle("基站+联网+google数据库定位");

mainView=newLinearLayout(this);

mainView.setOrientation(LinearLayout.VERTICAL);

button=newButton(this);

button.setText("定位测试");

button.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

(newHttpThread(context)).start();

}

});

mainView.addView(button,newLinearLayout.LayoutParams(-2,-2));

tv=newTextView(this);

tv.setText("Hello!

");

mainView.addView(tv);

setContentView(mainView);

}

classHttpThreadextendsThread{

TelephonyManagertm=null;

GsmCellLocationgcl=null;

intcid=0;

intlac=0;

intmcc=0;

intmnc=0;

StringBuffersb=null;

Handlerhandler=newHandler(){

publicvoidhandleMessage(Messagemsg){

switch(msg.what){

case1:

tv.append(sb.toString());

break;

}

super.handleMessage(msg);

}

};

HttpThread(Contextcontext){

tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

gcl=(GsmCellLocation)tm.getCellLocation();

cid=gcl.getCid();

lac=gcl.getLac();

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

mnc=Integer.valueOf(tm.getNetworkOperator().substring(3,5));

sb=newStringBuffer();

sb.append("cid:"+cid+"

");

sb.append("lac:"+lac+"

");

sb.append("mcc:"+mcc+"

");

sb.append("mnc:"+mnc+"

");

}

publicvoidrun(){

try{

JSONObjectjObject=newJSONObject();

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");

}else{

jObject.put("address_language","en_US");

}

JSONArrayjArray=newJSONArray();

JSONObjectjData=newJSONObject();

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);

DefaultHttpClientclient=newDefaultHttpClient();

HttpPostpost=newHttpPost("http://www.google.com/loc/json");

StringEntityse=newStringEntity(jObject.toString());

post.setEntity(se);

HttpResponseresp=client.execute(post);

BufferedReaderbr=null;

if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){

sb.append("联网成功

");

br=newBufferedReader(newInputStreamReader(resp.getEntity().getContent()));

}else{

sb.append("联网获取数据失败!

");

}

Stringresult=br.readLine();

while(result!=null){

sb.append(result);

result=br.readLine();

}

}catch(Exceptionex){

sb.append(ex.getMessage());

}

Messagemsg=newMessage();

msg.what=1;

handler.sendMessage(msg);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值