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

48 篇文章 1 订阅

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

  1. import java.io.BufferedReader; 
  2. import java.io.InputStreamReader; 
  3. import org.apache.http.HttpResponse; 
  4. import org.apache.http.HttpStatus; 
  5. import org.apache.http.client.methods.HttpPost; 
  6. import org.apache.http.entity.StringEntity; 
  7. import org.apache.http.impl.client.DefaultHttpClient; 
  8. import org.json.JSONArray; 
  9. import org.json.JSONObject; 
  10.  
  11. import android.app.Activity; 
  12. import android.content.Context; 
  13. import android.os.Bundle; 
  14. import android.os.Handler; 
  15. import android.os.Message; 
  16. import android.telephony.TelephonyManager; 
  17. import android.telephony.gsm.GsmCellLocation; 
  18. import android.view.View; 
  19. import android.view.View.OnClickListener; 
  20. import android.widget.Button; 
  21. import android.widget.LinearLayout; 
  22. import android.widget.TextView; 
  23.  
  24. public class Test extends Activity { 
  25.      
  26.     Context context=this
  27.     LinearLayout mainView=null
  28.     Button button=null
  29.     TextView tv=null
  30.      
  31.     public void onCreate(Bundle savedInstanceState) { 
  32.         super.onCreate(savedInstanceState); 
  33.         this.setTitle("基站+联网+google数据库定位"); 
  34.         mainView=new LinearLayout(this); 
  35.         mainView.setOrientation(LinearLayout.VERTICAL); 
  36.         button=new Button(this); 
  37.         button.setText("定位测试"); 
  38.         button.setOnClickListener(new OnClickListener(){ 
  39.             @Override 
  40.             public void onClick(View v) { 
  41.                 (new HttpThread(context)).start(); 
  42.             } 
  43.         }); 
  44.         mainView.addView(button,new LinearLayout.LayoutParams(-2,-2)); 
  45.         tv=new TextView(this); 
  46.         tv.setText("Hello!\n"); 
  47.         mainView.addView(tv); 
  48.         setContentView(mainView); 
  49.     } 
  50.          
  51.     class HttpThread extends Thread{ 
  52.          
  53.         TelephonyManager tm=null
  54.         GsmCellLocation gcl=null
  55.         int cid=0
  56.         int lac=0
  57.         int mcc = 0
  58.         int mnc =0
  59.         StringBuffer sb=null
  60.          
  61.         Handler handler=new Handler(){ 
  62.             public void handleMessage(Message msg) {    
  63.                 switch (msg.what) {      
  64.                 case 1
  65.                     tv.append(sb.toString()); 
  66.                     break
  67.                 } 
  68.                 super.handleMessage(msg); 
  69.            }    
  70.         }; 
  71.          
  72.         HttpThread(Context context){ 
  73.             tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
  74.             gcl=(GsmCellLocation) tm.getCellLocation(); 
  75.             cid=gcl.getCid(); 
  76.             lac=gcl.getLac(); 
  77.             mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3)); 
  78.             mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5)); 
  79.              
  80.             sb=new StringBuffer(); 
  81.             sb.append("cid:"+cid + "\n"); 
  82.             sb.append("lac:"+lac + "\n"); 
  83.             sb.append("mcc:"+mcc + "\n"); 
  84.             sb.append("mnc:"+mnc + "\n"); 
  85.         } 
  86.          
  87.         public void run(){ 
  88.             try
  89.                 JSONObject jObject = new JSONObject(); 
  90.                 jObject.put("version", "1.1.0"); 
  91.                 jObject.put("host", "maps.google.com"); 
  92.                 jObject.put("request_address", true); 
  93.                 if (mcc == 460) { 
  94.                     jObject.put("address_language", "zh_CN"); 
  95.                 } else
  96.                     jObject.put("address_language", "en_US"); 
  97.                 } 
  98.                 JSONArray jArray = new JSONArray(); 
  99.                 JSONObject jData = new JSONObject(); 
  100.                 jData.put("cell_id", cid); 
  101.                 jData.put("location_area_code", lac); 
  102.                 jData.put("mobile_country_code", mcc); 
  103.                 jData.put("mobile_network_code", mnc); 
  104.                 jArray.put(jData); 
  105.                 jObject.put("cell_towers", jArray); 
  106.                  
  107.                 DefaultHttpClient client = new DefaultHttpClient(); 
  108.                 HttpPost post = new HttpPost("http://www.google.com/loc/json"); 
  109.                 StringEntity se = new StringEntity(jObject.toString()); 
  110.                 post.setEntity(se); 
  111.                 HttpResponse resp = client.execute(post); 
  112.                 BufferedReader br = null
  113.                 if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
  114.                     sb.append("联网成功\n"); 
  115.                     br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent())); 
  116.                 }else
  117.                     sb.append("联网获取数据失败!\n"); 
  118.                 } 
  119.                  
  120.                 String result = br.readLine(); 
  121.                 while (result != null) { 
  122.                     sb.append(result); 
  123.                     result = br.readLine(); 
  124.                 } 
  125.             }catch(Exception ex){ 
  126.                 sb.append(ex.getMessage()); 
  127.             } 
  128.             Message msg=new Message(); 
  129.             msg.what=1
  130.             handler.sendMessage(msg); 
  131.         } 
  132.     } 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值