android 基站分布,android 基站定位

这里给大家分享下基站定位的实现,基站定位首先要通过TelephonyManager得到手机的信号信息,比如基站的国家编码,小区id等......得到这些后需要向google提供的接口提交这些参数,然后就会返回基站的相关信息。

废话不多说直接上代码吧:

importjava.io.Serializable;

importjava.util.ArrayList;

importjava.util.List;

importandroid.content.Context;

importandroid.telephony.NeighboringCellInfo;

importandroid.telephony.TelephonyManager;

importandroid.telephony.cdma.CdmaCellLocation;

importandroid.telephony.gsm.GsmCellLocation;

importandroid.util.Log;

/**

* @author yangzhiqiang

*

*/

publicclassCellIdInfoManagerimplementsSerializable {

/**

*

*/

privatestaticfinallongserialVersionUID = 5481154371450408380L;

privateContext context;

publicCellIdInfoManager(Context context) {

super();

this.context = context;

}

publicList getCellInfo() {

List listInfo =newArrayList();

intcountryCode;

intnetworkCode;

intareaCode;

CellInfo info =newCellInfo();

GsmCellLocation gsm =null;

TelephonyManager manager = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {

gsm = (GsmCellLocation) manager.getCellLocation();

if(gsm ==null) {

returnnull;

}

if(manager.getNetworkOperator() ==null

|| manager.getNetworkOperator().length() ==0) {

returnnull;

}

countryCode = Integer.parseInt(manager.getNetworkOperator()

.substring(0,3));

networkCode = Integer.parseInt(manager.getNetworkOperator()

.substring(3,5));

areaCode = gsm.getLac();

info.cellId = gsm.getCid();

info.mobileCountryCode = countryCode;

info.mobileNetworkCode = networkCode;

info.locationAreaCode = areaCode;

info.radio_type ="gsm";

listInfo.add(info);

List list = manager.getNeighboringCellInfo();

for(NeighboringCellInfo i : list) {

CellInfo ci =newCellInfo();

ci.cellId = i.getCid();

ci.mobileCountryCode = countryCode;

ci.mobileNetworkCode = networkCode;

ci.locationAreaCode = areaCode;

ci.radio_type ="gsm";

listInfo.add(ci);

}

}elseif(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {

CdmaCellLocation cdma = (CdmaCellLocation) manager

.getCellLocation();

if(cdma ==null) {

returnnull;

}

if(manager.getNetworkOperator() ==null

|| manager.getNetworkOperator().length() ==0) {

returnnull;

}

Log.v("TAG","CDMA");

info.cellId = cdma.getBaseStationId();

info.mobileCountryCode = Integer.parseInt(manager

.getNetworkOperator());

info.mobileNetworkCode = cdma.getSystemId();

info.locationAreaCode = cdma.getNetworkId();

info.radio_type ="cdma";

listInfo.add(info);

}

returnlistInfo;

}

publicclassCellInfo {

// 基站编号

publicintcellId;

// 国家代码

publicintmobileCountryCode;

// 网络代码

publicintmobileNetworkCode;

// 区域代码

publicintlocationAreaCode;

publicString radio_type;

publicCellInfo() {

super();

}

}

}

import java.io.Serializable;

import java.util.ArrayList;

import java.util.List;

import android.content.Context;

import android.telephony.NeighboringCellInfo;

import android.telephony.TelephonyManager;

import android.telephony.cdma.CdmaCellLocation;

import android.telephony.gsm.GsmCellLocation;

import android.util.Log;

/**

* @author yangzhiqiang

*

*/

public class CellIdInfoManager implements Serializable {

/**

*

*/

private static final long serialVersionUID = 5481154371450408380L;

private Context context;

public CellIdInfoManager(Context context) {

super();

this.context = context;

}

public List getCellInfo() {

List listInfo = new ArrayList();

int countryCode;

int networkCode;

int areaCode;

CellInfo info = new CellInfo();

GsmCellLocation gsm = null;

TelephonyManager manager = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {

gsm = (GsmCellLocation) manager.getCellLocation();

if (gsm == null) {

return null;

}

if (manager.getNetworkOperator() == null

|| manager.getNetworkOperator().length() == 0) {

return null;

}

countryCode = Integer.parseInt(manager.getNetworkOperator()

.substring(0, 3));

networkCode = Integer.parseInt(manager.getNetworkOperator()

.substring(3, 5));

areaCode = gsm.getLac();

info.cellId = gsm.getCid();

info.mobileCountryCode = countryCode;

info.mobileNetworkCode = networkCode;

info.locationAreaCode = areaCode;

info.radio_type = "gsm";

listInfo.add(info);

List list = manager.getNeighboringCellInfo();

for (NeighboringCellInfo i : list) {

CellInfo ci = new CellInfo();

ci.cellId = i.getCid();

ci.mobileCountryCode = countryCode;

ci.mobileNetworkCode = networkCode;

ci.locationAreaCode = areaCode;

ci.radio_type = "gsm";

listInfo.add(ci);

}

} else if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {

CdmaCellLocation cdma = (CdmaCellLocation) manager

.getCellLocation();

if (cdma == null) {

return null;

}

if (manager.getNetworkOperator() == null

|| manager.getNetworkOperator().length() == 0) {

return null;

}

Log.v("TAG", "CDMA");

info.cellId = cdma.getBaseStationId();

info.mobileCountryCode = Integer.parseInt(manager

.getNetworkOperator());

info.mobileNetworkCode = cdma.getSystemId();

info.locationAreaCode = cdma.getNetworkId();

info.radio_type = "cdma";

listInfo.add(info);

}

return listInfo;

}

public class CellInfo {

// 基站编号

public int cellId;

// 国家代码

public int mobileCountryCode;

// 网络代码

public int mobileNetworkCode;

// 区域代码

public int locationAreaCode;

public String radio_type;

public CellInfo() {

super();

}

}

}

上面是得到手机信号的信息,下面是将这些信息发送到google服务器并解析结果:

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importjava.io.Serializable;

importjava.util.List;

importorg.apache.http.HttpEntity;

importorg.apache.http.HttpResponse;

importorg.apache.http.HttpStatus;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.entity.StringEntity;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.json.JSONArray;

importorg.json.JSONObject;

importandroid.location.Location;

importandroid.util.Log;

importcom.metarnet.gps.CellIdInfoManager.CellInfo;

/**

* @author Administrator

*

*/

publicclassNetworkLocationManagerimplementsSerializable {

/**

*

*/

privatestaticfinallongserialVersionUID = 1185788569820321281L;

publicstaticLocation getBaseStationLocation(List cellID) {

if(cellID ==null) {

Log.i("TAG","cellId is null.");

returnnull;

}

DefaultHttpClient client =newDefaultHttpClient();

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

JSONObject holder =newJSONObject();

try{

CellInfo info = cellID.get(0);

holder.put("version","1.1.0");

holder.put("host","maps.google.com");

holder.put("home_mobile_country_code", info.mobileCountryCode);

holder.put("home_mobile_network_code", info.mobileNetworkCode);

holder.put("request_address",true);

holder.put("radio_type", info.radio_type);

if("460".equals(info.mobileCountryCode)) {

holder.put("address_language","zh_CN");

}else{

holder.put("address_language","en_US");

}

JSONObject data, current_data;

JSONArray array =newJSONArray();

current_data =newJSONObject();

current_data.put("cell_id", info.cellId);

current_data.put("location_area_code", info.locationAreaCode);

current_data.put("mobile_country_code", info.mobileCountryCode);

current_data.put("mobile_network_code", info.mobileNetworkCode);

current_data.put("age",0);

array.put(current_data);

if(cellID.size() >2) {

for(inti =1; i 

data =newJSONObject();

data.put("cell_id", info.cellId);

data.put("location_area_code", info.locationAreaCode);

data.put("mobile_country_code", info.mobileCountryCode);

data.put("mobile_network_code", info.mobileNetworkCode);

data.put("age",0);

array.put(data);

}

}

holder.put("cell_towers", array);

StringEntity se =newStringEntity(holder.toString());

post.setEntity(se);

HttpResponse resp = client.execute(post);

intstate = resp.getStatusLine().getStatusCode();

if(state == HttpStatus.SC_OK) {

HttpEntity entity = resp.getEntity();

if(entity !=null) {

BufferedReader br =newBufferedReader(

newInputStreamReader(entity.getContent()));

StringBuffer sb =newStringBuffer();

String resute ="";

while((resute = br.readLine()) !=null) {

sb.append(resute);

}

br.close();

data =newJSONObject(sb.toString());

data = (JSONObject) data.get("location");

Location loc =newLocation(

android.location.LocationManager.NETWORK_PROVIDER);

loc.setLatitude((Double) data.get("latitude"));

loc.setLongitude((Double) data.get("longitude"));

loc.setAccuracy(Float.parseFloat(data.get("accuracy")

.toString()));

loc.setTime(System.currentTimeMillis());

returnloc;

}else{

returnnull;

}

}else{

Log.v("TAG", state +"");

returnnull;

}

}catch(Exception e) {

Log.e("TAG", e.getMessage());

returnnull;

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值