在真机上测试:
要点一: 一定要插手机卡 ---得到cid通过移动
要点二: 一定要能上网 ----得到经纬度---通过google
public class BaseStationManager {
Context c;
private TelephonyManager phoneService;
String json = "aaa";
private static volatile BaseStationManager bsManager ;
private BaseStationManager(Context c){
this.c = c;
}
public static BaseStationManager getInstance(Context c){
if(bsManager == null){
synchronized(BaseStationManager.class){
if(bsManager == null){
bsManager = new BaseStationManager(c);
}
}
}//if
return bsManager;
}
//=======================================================================================
public void init(){
phoneService = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
update();
}
public String getLatitude(){
String latitude = "null";
Pattern p = Pattern.compile("latitude\":[^,]+");
Matcher m = p.matcher(json);
if(m.find()){
latitude = m.group().split(":")[1];
}
return latitude;
}
public String getLongitude(){
String latitude = "null";
Pattern p = Pattern.compile("longitude\":[^,]+");
Matcher m = p.matcher(json);
if(m.find()){
latitude = m.group().split(":")[1];
}
return latitude;
}
public void update(){
GsmCellLocation gcl = (GsmCellLocation) phoneService.getCellLocation();
int cid = gcl.getCid();//CID for GSM
int lac = gcl.getLac();
int mcc = Integer.valueOf(phoneService.getNetworkOperator().substring(0,3));
int mnc = Integer.valueOf(phoneService.getNetworkOperator().substring(3,5));
json = "bbb";
try {
//组装JSON查询字符串
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);
//=============json封装成StringEntity,StringEntity让入post, post让client执行
// 创建连接,发送请求并接受回应
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);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String l = null;
while ((l=br.readLine()) != null) {
sb.append(l);
}
json = sb.toString();
}catch(Exception e){
}
}
}
分享到:
相关推荐
VBA代码封装助手VBA代码封装助手
核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装 核心代码封装
百度基站定位的实现,封装好的代码,可以参考一下。
VBA代码封装完整示例.rarVBA代码封装完整示例.rarVBA代码封装完整示例.rar
VBA代码封装过程.pdf
sqlite3 dll源代码 静态库源代码 封装包源代码 涵盖sqlite3所有源代码,可以任意修改 才2个积分,绝对超值
Excel宏代码封装实例代码.将需要保护的代码封装为dll
有感于制作网站时,常常要求有分页功能。为了避免写重复又不复杂的代码,特编写的这个关于sqlserver 的java代码封装
libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++封装最新代码libuv c++...
Swift iOS StoreKit 原生内购订阅代码封装,语言是Swift,内含demo,可以二次封装给其他开发工具使用.
简化Socket的应用编程,将此封装成库
裸系统封装代码;裸系统封装代码;裸系统封装代码;裸系统封装代码;
使用C++代码封装的win32操作类, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using ...
这里面有c#代码如何实现封装的具体步骤,大家可以参考一下啊!具体的课参考里面
基于iOS系统定位封装,封装成单例,可直接调用 //获取经纬度回调 typedef void(^LocationToolData)(NSString*longitude,NSString*latitude); @property (nonatomic,copy)LocationToolData longitudeAndLatitude;//...
在Windows上是使用系统win32 api创建和管理线程,Linux和Mac通常使用pthread,尽管Windows也可以使用第三方的pthread库,但这样库的依赖会比较多,项目的部署会麻烦些,最佳的方法应该还是写跨平台代码,通过宏区分...
java封装好的分页代码
AndroidLearning 安卓封装的学习 封装activity路由 封装activitytrans动画库增加最新5.0支持的共享元素动画 Dagger2
利用高德地图的sdk完成实时定位的功能,方法的封装,简单易懂。
开源excel操作库 代码可封装 好用 开源excel操作库 代码可封装 好用