`
helpbs
  • 浏览: 1166118 次
文章分类
社区版块
存档分类
最新评论

Android基站+联网+google数据库定位

 
阅读更多

(原文主要方法转载于百度文库:http://wenku.baidu.com/view/2220dde8172ded630b1cb6ee.html

  1. importjava.io.BufferedReader;
  2. importjava.io.InputStreamReader;
  3. importorg.apache.http.HttpResponse;
  4. importorg.apache.http.HttpStatus;
  5. importorg.apache.http.client.methods.HttpPost;
  6. importorg.apache.http.entity.StringEntity;
  7. importorg.apache.http.impl.client.DefaultHttpClient;
  8. importorg.json.JSONArray;
  9. importorg.json.JSONObject;
  10. importandroid.app.Activity;
  11. importandroid.content.Context;
  12. importandroid.os.Bundle;
  13. importandroid.os.Handler;
  14. importandroid.os.Message;
  15. importandroid.telephony.TelephonyManager;
  16. importandroid.telephony.gsm.GsmCellLocation;
  17. importandroid.view.View;
  18. importandroid.view.View.OnClickListener;
  19. importandroid.widget.Button;
  20. importandroid.widget.LinearLayout;
  21. importandroid.widget.TextView;
  22. publicclassTestextendsActivity{
  23. Contextcontext=this;
  24. LinearLayoutmainView=null;
  25. Buttonbutton=null;
  26. TextViewtv=null;
  27. publicvoidonCreate(BundlesavedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. this.setTitle("基站+联网+google数据库定位");
  30. mainView=newLinearLayout(this);
  31. mainView.setOrientation(LinearLayout.VERTICAL);
  32. button=newButton(this);
  33. button.setText("定位测试");
  34. button.setOnClickListener(newOnClickListener(){
  35. @Override
  36. publicvoidonClick(Viewv){
  37. (newHttpThread(context)).start();
  38. }
  39. });
  40. mainView.addView(button,newLinearLayout.LayoutParams(-2,-2));
  41. tv=newTextView(this);
  42. tv.setText("Hello!\n");
  43. mainView.addView(tv);
  44. setContentView(mainView);
  45. }
  46. classHttpThreadextendsThread{
  47. TelephonyManagertm=null;
  48. GsmCellLocationgcl=null;
  49. intcid=0;
  50. intlac=0;
  51. intmcc=0;
  52. intmnc=0;
  53. StringBuffersb=null;
  54. Handlerhandler=newHandler(){
  55. publicvoidhandleMessage(Messagemsg){
  56. switch(msg.what){
  57. case1:
  58. tv.append(sb.toString());
  59. break;
  60. }
  61. super.handleMessage(msg);
  62. }
  63. };
  64. HttpThread(Contextcontext){
  65. tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
  66. gcl=(GsmCellLocation)tm.getCellLocation();
  67. cid=gcl.getCid();
  68. lac=gcl.getLac();
  69. mcc=Integer.valueOf(tm.getNetworkOperator().substring(0,3));
  70. mnc=Integer.valueOf(tm.getNetworkOperator().substring(3,5));
  71. sb=newStringBuffer();
  72. sb.append("cid:"+cid+"\n");
  73. sb.append("lac:"+lac+"\n");
  74. sb.append("mcc:"+mcc+"\n");
  75. sb.append("mnc:"+mnc+"\n");
  76. }
  77. publicvoidrun(){
  78. try{
  79. JSONObjectjObject=newJSONObject();
  80. jObject.put("version","1.1.0");
  81. jObject.put("host","maps.google.com");
  82. jObject.put("request_address",true);
  83. if(mcc==460){
  84. jObject.put("address_language","zh_CN");
  85. }else{
  86. jObject.put("address_language","en_US");
  87. }
  88. JSONArrayjArray=newJSONArray();
  89. JSONObjectjData=newJSONObject();
  90. jData.put("cell_id",cid);
  91. jData.put("location_area_code",lac);
  92. jData.put("mobile_country_code",mcc);
  93. jData.put("mobile_network_code",mnc);
  94. jArray.put(jData);
  95. jObject.put("cell_towers",jArray);
  96. DefaultHttpClientclient=newDefaultHttpClient();
  97. HttpPostpost=newHttpPost("http://www.google.com/loc/json");
  98. StringEntityse=newStringEntity(jObject.toString());
  99. post.setEntity(se);
  100. HttpResponseresp=client.execute(post);
  101. BufferedReaderbr=null;
  102. if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
  103. sb.append("联网成功\n");
  104. br=newBufferedReader(newInputStreamReader(resp.getEntity().getContent()));
  105. }else{
  106. sb.append("联网获取数据失败!\n");
  107. }
  108. Stringresult=br.readLine();
  109. while(result!=null){
  110. sb.append(result);
  111. result=br.readLine();
  112. }
  113. }catch(Exceptionex){
  114. sb.append(ex.getMessage());
  115. }
  116. Messagemsg=newMessage();
  117. msg.what=1;
  118. handler.sendMessage(msg);
  119. }
  120. }
  121. }

所需权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics