Android基站定位原理及实现代码
程序员文章站
2024-02-09 19:25:28
复制代码 代码如下: import java.io.bufferedreader; import java.io.inputstreamreader; import org...
复制代码 代码如下:
import java.io.bufferedreader;
import java.io.inputstreamreader;
import org.apache.http.httpresponse;
import org.apache.http.httpstatus;
import org.apache.http.client.methods.httppost;
import org.apache.http.entity.stringentity;
import org.apache.http.impl.client.defaulthttpclient;
import org.json.jsonarray;
import org.json.jsonobject;
import android.app.activity;
import android.content.context;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.telephony.telephonymanager;
import android.telephony.gsm.gsmcelllocation;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.linearlayout;
import android.widget.textview;
public class test extends activity {
context context=this;
linearlayout mainview=null;
button button=null;
textview tv=null;
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
this.settitle("基站+联网+google数据库定位");
mainview=new linearlayout(this);
mainview.setorientation(linearlayout.vertical);
button=new button(this);
button.settext("定位测试");
button.setonclicklistener(new onclicklistener(){
@override
public void onclick(view v) {
(new httpthread(context)).start();
}
});
mainview.addview(button,new linearlayout.layoutparams(-2,-2));
tv=new textview(this);
tv.settext("hello!\n");
mainview.addview(tv);
setcontentview(mainview);
}
class httpthread extends thread{
telephonymanager tm=null;
gsmcelllocation gcl=null;
int cid=0;
int lac=0;
int mcc = 0;
int mnc =0;
stringbuffer sb=null;
handler handler=new handler(){
public void handlemessage(message msg) {
switch (msg.what) {
case 1:
tv.append(sb.tostring());
break;
}
super.handlemessage(msg);
}
};
httpthread(context context){
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=new stringbuffer();
sb.append("cid:"+cid + "\n");
sb.append("lac:"+lac + "\n");
sb.append("mcc:"+mcc + "\n");
sb.append("mnc:"+mnc + "\n");
}
public void run(){
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");
} else {
jobject.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) {
sb.append("联网成功\n");
br = new bufferedreader(new inputstreamreader(resp.getentity().getcontent()));
}else{
sb.append("联网获取数据失败!\n");
}
string result = br.readline();
while (result != null) {
sb.append(result);
result = br.readline();
}
}catch(exception ex){
sb.append(ex.getmessage());
}
message msg=new message();
msg.what=1;
handler.sendmessage(msg);
}
}
}
所需权限
复制代码 代码如下:
<uses-permission android:name="android.permission.internet"></uses-permission>
< uses-permission android:name="android.permission.access_coarse_location"></uses-permission>
上一篇: 浅谈Eclipse PDT调试PHP程序