基站定位示例源码

  1. package cn.MyAndroid.Test;

  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;

  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;

  11. import android.app.Activity;
  12. import android.content.Context;
  13. import android.os.Bundle;
  14. import android.telephony.TelephonyManager;
  15. import android.telephony.gsm.GsmCellLocation;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.TextView;

  19. public class MyLocation extends Activity {
  20. TextView mTextView;
  21. Button mButton;
  22. TelephonyManager tm;

  23. /** Called when the activity is first created. */
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);

  28. mTextView = (TextView) findViewById(R.id.TextView01);
  29. mButton = (Button) findViewById(R.id.Button01);
  30. tm = (TelephonyManager) this
  31. .getSystemService(Context.TELEPHONY_SERVICE);

  32. mButton.setOnClickListener(new Button.OnClickListener() {

  33. @Override
  34. public void onClick(View v) {
  35. // TODO Auto-generated method stub
  36. GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
  37. int cid = gcl.getCid();
  38. int lac = gcl.getLac();

  39. int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,
  40. 3));
  41. int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,
  42. 5));
  43. try {
  44. //组装JSON查询字符串
  45. JSONObject holder = new JSONObject();
  46. holder.put("version", "1.1.0");
  47. holder.put("host", "maps.google.com");
  48. // holder.put("address_language", "zh_CN");
  49. holder.put("request_address", true);

  50. JSONArray array = new JSONArray();
  51. JSONObject data = new JSONObject();
  52. data.put("cell_id", cid); // 25070
  53. data.put("location_area_code", lac);// 4474
  54. data.put("mobile_country_code", mcc);// 460
  55. data.put("mobile_network_code", mnc);// 0
  56. array.put(data);
  57. holder.put("cell_towers", array);

  58. // 创建连接,发送请求并接受回应
  59. DefaultHttpClient client = new DefaultHttpClient();

  60. HttpPost post = new HttpPost(
  61. "http://www.google.com/loc/json");

  62. StringEntity se = new StringEntity(holder.toString());

  63. post.setEntity(se);
  64. HttpResponse resp = client.execute(post);

  65. HttpEntity entity = resp.getEntity();

  66. BufferedReader br = new BufferedReader(
  67. new InputStreamReader(entity.getContent()));
  68. StringBuffer sb = new StringBuffer();
  69. String result = br.readLine();

  70. while (result != null) {

  71. sb.append(result);
  72. result = br.readLine();
  73. }

  74. mTextView.setText(sb.toString());
  75. } catch (Exception e) {
  76. // TODO: handle exception
  77. }

  78. }

  79. });
  80. }
  81. }
复制代码


注意要给Activity加入以下权限:<uses-permission android:name="android.permission.PERMISSION_NAME" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
关于基站查询协议的话,请参考这个链接: http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值