Android开发笔记之简单基站定位程序的实现

2020-01-18 16:47:31丽君

现在在onBtnClick方法中编码,依次调用后面几个方法,代码如下:


/** 按钮点击回调函数 */
private void onBtnClick(){
  /** 弹出一个等待状态的框 */
  ProgressDialog mProgressDialog = new ProgressDialog(this);
  mProgressDialog.setMessage("正在获取中...");
  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  mProgressDialog.show();
   
  try {
    /** 获取基站数据 */
    SCell cell = getCellInfo();
     
    /** 根据基站数据获取经纬度 */
    SItude itude = getItude(cell);
     
    /** 获取地理位置 */
    String location = getLocation(itude);
     
    /** 显示结果 */
    showResult(cell, location);
     
    /** 关闭对话框 */
    mProgressDialog.dismiss();
  }catch (Exception e) {
    /** 关闭对话框 */
    mProgressDialog.dismiss();
    /** 显示错误 */
    TextView cellText = (TextView)findViewById(R.id.cellText);
    cellText.setText(e.getMessage());
  }
}

按钮相关的工作就完成了,接下来编写获取基站信息的方法。

三、获取基站信息

获取基站信息我们需要调用SDK提供的API中的TelephonyManager,需要在文件头部引入:


import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

完整代码为:


/**
 * 获取基站信息
 * 
 * @throws Exception
 */
private SCell getCellInfo() throws Exception {
  SCell cell = new SCell();
 
  /** 调用API获取基站信息 */
  TelephonyManager mTelNet = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();
  if (location == null)
    throw new Exception("获取基站信息失败");
 
  String operator = mTelNet.getNetworkOperator();
  int mcc = Integer.parseInt(operator.substring(0, 3));
  int mnc = Integer.parseInt(operator.substring(3));
  int cid = location.getCid();
  int lac = location.getLac();
 
  /** 将获得的数据放到结构体中 */
  cell.MCC = mcc;
  cell.MNC = mnc;
  cell.LAC = lac;
  cell.CID = cid;
 
  return cell;
}

如果获得的位置信息为null将抛出错误,不再继续执行。最后将获取的基站信息封装为结构体返回。

四、获取经纬度

在这一步,我们需要采用HTTP调用google的API以获取基站所在的经纬度。

Android作为一款互联网手机,联网的功能必不可少。Android提供了多个接口供我们使用,这里我们使用DefaultHttpClient。

完整的方法代码如下:


/**
 * 获取经纬度
 * 
 * @throws Exception
 */
private SItude getItude(SCell cell) throws Exception {
  SItude itude = new SItude();
 
  /** 采用Android默认的HttpClient */
  HttpClient client = new DefaultHttpClient();
  /** 采用POST方法 */
  HttpPost post = new HttpPost("http://www.easck.com/loc/json");
  try {
    /** 构造POST的JSON数据 */
    JSONObject holder = new JSONObject();
    holder.put("version", "1.1.0");
    holder.put("host", "maps.google.com");
    holder.put("address_language", "zh_CN");
    holder.put("request_address", true);
    holder.put("radio_type", "gsm");
    holder.put("carrier", "HTC");
 
    JSONObject tower = new JSONObject();
    tower.put("mobile_country_code", cell.MCC);
    tower.put("mobile_network_code", cell.MNC);
    tower.put("cell_id", cell.CID);
    tower.put("location_area_code", cell.LAC);
 
    JSONArray towerarray = new JSONArray();
    towerarray.put(tower);
    holder.put("cell_towers", towerarray);
 
    StringEntity query = new StringEntity(holder.toString());
    post.setEntity(query);
 
    /** 发出POST数据并获取返回数据 */
    HttpResponse response = client.execute(post);
    HttpEntity entity = response.getEntity();
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
    StringBuffer strBuff = new StringBuffer();
    String result = null;
    while ((result = buffReader.readLine()) != null) {
      strBuff.append(result);
    }
 
    /** 解析返回的JSON数据获得经纬度 */
    JSONObject json = new JSONObject(strBuff.toString());
    JSONObject subjosn = new JSONObject(json.getString("location"));
 
    itude.latitude = subjosn.getString("latitude");
    itude.longitude = subjosn.getString("longitude");
     
    Log.i("Itude", itude.latitude + itude.longitude);
     
  } catch (Exception e) {
    Log.e(e.getMessage(), e.toString());
    throw new Exception("获取经纬度出现错误:"+e.getMessage());
  } finally{
    post.abort();
    client = null;
  }
   
  return itude;
}