歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android獲取基站坐標代碼

Android獲取基站坐標代碼

日期:2017/3/1 10:22:22   编辑:Linux編程

Android獲取基站坐標代碼:

  1. package com.su.station;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.entity.StringEntity;
  9. import org.apache.http.impl.client.DefaultHttpClient;
  10. import org.json.JSONArray;
  11. import org.json.JSONObject;
  12. import android.app.Activity;
  13. import android.content.Context;
  14. import android.location.Location;
  15. import android.location.LocationManager;
  16. import android.os.Bundle;
  17. import android.telephony.TelephonyManager;
  18. import android.telephony.gsm.GsmCellLocation;
  19. import android.util.Log;
  20. import android.widget.Toast;
  21. public class TestStationLocationActivity extends Activity {
  22. private static final String TAG = "TestStationLocationActivity";
  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. Location location = getportLocation();
  29. location.getLongitude();
  30. Toast.makeText(this, location.getLatitude()+location.getLatitude()+"", 100).show();
  31. }
  32. private Location getportLocation() {
  33. Location loc = null ;
  34. TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  35. BufferedReader br = null;
  36. try
  37. {
  38. GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
  39. if (null == gcl)
  40. {
  41. return null;
  42. }
  43. int cid = gcl.getCid();
  44. int lac = gcl.getLac();
  45. int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));
  46. int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));
  47. JSONObject holder = new JSONObject();
  48. holder.put("version", "1.1.0");
  49. holder.put("host", "maps.google.com");
  50. holder.put("request_address", true);
  51. JSONArray array = new JSONArray();
  52. JSONObject data = new JSONObject();
  53. data.put("cell_id", cid);
  54. data.put("location_area_code", lac);
  55. data.put("mobile_country_code", mcc);
  56. data.put("mobile_network_code", mnc);
  57. array.put(data);
  58. holder.put("cell_towers", array);
  59. DefaultHttpClient client = new DefaultHttpClient();
  60. HttpPost post = new HttpPost("http://www.google.com/loc/json");
  61. StringEntity se = new StringEntity(holder.toString());
  62. post.setEntity(se);
  63. HttpResponse resp = client.execute(post);
  64. if (resp.getStatusLine().getStatusCode() == 200)
  65. {
  66. HttpEntity entity = resp.getEntity();
  67. br = new BufferedReader(new InputStreamReader(entity.getContent()));
  68. StringBuffer sb = new StringBuffer();
  69. String result = br.readLine();
  70. while (result != null)
  71. {
  72. sb.append(result);
  73. result = br.readLine();
  74. }
  75. JSONObject data_ = new JSONObject(sb.toString());
  76. data_ = (JSONObject) data_.get("location");
  77. loc = new Location(LocationManager.NETWORK_PROVIDER);
  78. loc.setLatitude((Double) data_.get("latitude"));
  79. loc.setLongitude((Double) data_.get("longitude"));
  80. Log.i(TAG, "latitude : " + loc.getLatitude() + " longitude : " + loc.getLongitude());
  81. return loc;
  82. }
  83. return null;
  84. }
  85. catch (Exception e)
  86. {
  87. android.util.Log.e(TAG, "network get the latitude and longitude ocurr Exception error", e);
  88. }
  89. finally
  90. {
  91. if (null != br)
  92. {
  93. try
  94. {
  95. br.close();
  96. }
  97. catch (IOException e)
  98. {
  99. android.util.Log.e(TAG, "network get the latitude and longitude when closed BufferedReader ocurr IOException error", e);
  100. }
  101. }
  102. }
  103. return loc;
  104. }
  105. }

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved