加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android获取基站坐标代码

发布时间:2020-12-15 03:20:03 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; import org.apache.http.HttpEntity;import org.apac

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.widget.Toast;
 
public class TestStationLocationActivity extends Activity {
    private static final String TAG = "TestStationLocationActivity";
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Location location = getportLocation();
        location.getLongitude();
        Toast.makeText(this,location.getLatitude()+location.getLatitude()+"",100).show();
         
    }
     
    private Location getportLocation() {
        Location loc = null ;
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        BufferedReader br = null;
        try
        {   
            GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
                if (null == gcl)
                {
                        return null;
                }
                    int cid = gcl.getCid();
                    int lac = gcl.getLac();
                    int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));
                    int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));
                    JSONObject holder = new JSONObject();
                    holder.put("version","1.1.0");
                    holder.put("host","maps.google.com");
                    holder.put("request_address",true);
                     
                    JSONArray array = new JSONArray();
                    JSONObject data = new JSONObject();
                     
                    data.put("cell_id",cid);
                    data.put("location_area_code",lac);
                    data.put("mobile_country_code",mcc);
                    data.put("mobile_network_code",mnc);
                    array.put(data);
                    holder.put("cell_towers",array);
                    DefaultHttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost("http://www.google.com/loc/json");
                    StringEntity se = new StringEntity(holder.toString());
                    post.setEntity(se);
                    HttpResponse resp = client.execute(post);
                    if (resp.getStatusLine().getStatusCode() == 200) 
                {
                        HttpEntity entity = resp.getEntity();
                            br = new BufferedReader(new InputStreamReader(entity.getContent()));
                            StringBuffer sb = new StringBuffer();
                            String result = br.readLine();
                            while (result != null) 
                            {
                                    sb.append(result);
                                    result = br.readLine();
                            }
                             
                            JSONObject data_ = new JSONObject(sb.toString());
                            data_ = (JSONObject) data_.get("location");
                        loc = new Location(LocationManager.NETWORK_PROVIDER);
                        loc.setLatitude((Double) data_.get("latitude"));
                        loc.setLongitude((Double) data_.get("longitude"));
                        Log.i(TAG,"latitude : " + loc.getLatitude() + "  longitude : " + loc.getLongitude());
                        return loc;
                }
                return null;
 
        } 
         
        catch (Exception e) 
        {
                android.util.Log.e(TAG,"network get the latitude and longitude ocurr Exception error",e);
        }
        finally
        {
                if (null != br)
                {
                        try
                        {
                                br.close();
                        } 
                        catch (IOException e) 
                        {
                                android.util.Log.e(TAG,"network get the latitude and longitude when closed BufferedReader ocurr IOException error",e);
                        }
                }
        }
        return loc;
    }
 
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读