这次为大家讲到的是通过运营商的基站定位
这块主要是移动联通的基站,电信也是同样道理不过需要使用另外的接口去调用。
代码中是使用了我在聚合数据中API接口
*备注:Http通信框架android-async-http.jar
解析Json数据的GSON框架
如果想要Jar以及工具类请留言*
注:权限:
<uses-permission android:name="android.permission.INTERNET"/>
package com.camerademo.service;
import org.apache.http.Header;
import org.json.JSONObject;
import com.camerademo.util.HttpClient;
import com.camerademo.util.JsonUtil;
import com.camerademo.util.Result;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.widget.Toast;
/**
* APN
* 基站定位
* @author Howie
* @date 2015-12-03
*/
public class CellService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
startCell();
getSItude(getCellInfo());
//通过Cell基站定位
// new Thread(new Runnable() {
// @Override
// public void run() {
// // TODO Auto-generated method stub
// try {
// getItude(getCellInfo());
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }).start();
}
/**
* 聚合数据提供的接口
* @param cell
*/
private void getSItude(SCell cell){
String url = "http://v.juhe.cn/cell/get";
//mnc=0&cell=28655&lac=17695&key=您申请的APPKEY
RequestParams params = new RequestParams();
params.put("mnc", cell.MNC);
params.put("cell", cell.CID);
params.put("lac", cell.LAC);
params.put("key", "65c0033b14726506e757b41f51bc34c4");
HttpClient.get(url, params, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, headers, response);
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
Log.e("CellService", response.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers,
Throwable throwable, JSONObject errorResponse) {
// TODO Auto-generated method stub
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.e("CellService经度:", "fail");
}
});
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private TelephonyManager telManager;
private void startCell(){
telManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
// List<CellInfo> cells = telManager.getAllCellInfo();
}
private SCell getCellInfo(){
SCell cell = new SCell();
GsmCellLocation cellLocaiton = (GsmCellLocation)telManager.getCellLocation();
if (cellLocaiton == null)
Log.e("CellService","获取基站信息失败");
String operator = telManager.getNetworkOperator();
/** 将获得的数据放到结构体中 */
cell.MCC = Integer.parseInt(operator.substring(0, 3));;
cell.MNC = Integer.parseInt(operator.substring(3));
cell.LAC = cellLocaiton.getLac();
cell.CID = cellLocaiton.getCid();
return cell;
}
/**
* 获取经纬度
*
* @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.google.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;
}
}
/*
* 基站信息结构体
* MCC,Mobile Country Code,移动国家代码(中国的为460)
* MNC,Mobile Network Code,移动网络号码(中国移动为00,中国联通为01)
* LAC,Location Area Code,位置区域码
* CID,Cell Identity,基站编号,是个16位的数据(范围是0到65535)
*/
class SCell{
public int MCC;
public int MNC;
public int LAC;
public int CID;
}
/** 经纬度信息结构体 */
class SItude{
public String latitude;
public String longitude;
}