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