Android基站定位原理及实现代码

天无罪

天无罪

2016-02-19 10:28

下面图老师小编跟大家分享Android基站定位原理及实现代码,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~
代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Test extends Activity {

Context context=this;
LinearLayout mainView=null;
Button button=null;
TextView tv=null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("基站+联网+google数据库定位");
mainView=new LinearLayout(this);
mainView.setOrientation(LinearLayout.VERTICAL);
button=new Button(this);
button.setText("定位测试");
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
(new HttpThread(context)).start();
}
});
mainView.addView(button,new LinearLayout.LayoutParams(-2,-2));
tv=new TextView(this);
tv.setText("Hello!n");
mainView.addView(tv);
setContentView(mainView);
}

class HttpThread extends Thread{

TelephonyManager tm=null;
GsmCellLocation gcl=null;
int cid=0;
int lac=0;
int mcc = 0;
int mnc =0;
StringBuffer sb=null;

Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
tv.append(sb.toString());
break;
}
super.handleMessage(msg);
}
};

HttpThread(Context context){
tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
gcl=(GsmCellLocation) tm.getCellLocation();
cid=gcl.getCid();
lac=gcl.getLac();
mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));
mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));

sb=new StringBuffer();
sb.append("cid:"+cid + "n");
sb.append("lac:"+lac + "n");
sb.append("mcc:"+mcc + "n");
sb.append("mnc:"+mnc + "n");
}

public void run(){
try {
JSONObject jObject = new JSONObject();
jObject.put("version", "1.1.0");
jObject.put("host", "maps.google.com");
jObject.put("request_address", true);
if (mcc == 460) {
jObject.put("address_language", "zh_CN");
} else {
jObject.put("address_language", "en_US");
}
JSONArray jArray = new JSONArray();
JSONObject jData = new JSONObject();
jData.put("cell_id", cid);
jData.put("location_area_code", lac);
jData.put("mobile_country_code", mcc);
jData.put("mobile_network_code", mnc);
jArray.put(jData);
jObject.put("cell_towers", jArray);

DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.google.com/loc/json");
StringEntity se = new StringEntity(jObject.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
BufferedReader br = null;
if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
sb.append("联网成功n");
br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
}else{
sb.append("联网获取数据失败!n");
}

String result = br.readLine();
while (result != null) {
sb.append(result);
result = br.readLine();
}
}catch(Exception ex){
sb.append(ex.getMessage());
}
Message msg=new Message();
msg.what=1;
handler.sendMessage(msg);
}
}
}

所需权限
代码如下:

uses-permission android:name="android.permission.INTERNET"/uses-permission
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/uses-permission
展开更多 50%)
分享

猜你喜欢

Android基站定位原理及实现代码

编程语言 网络编程
Android基站定位原理及实现代码

android IntentService实现原理及内部代码分享

编程语言 网络编程
android IntentService实现原理及内部代码分享

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

android手机获取gps和基站的经纬度地址实现代码

编程语言 网络编程
android手机获取gps和基站的经纬度地址实现代码

android panellistview 圆角实现代码

编程语言 网络编程
android panellistview 圆角实现代码

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

android屏幕全屏的实现代码

编程语言 网络编程
android屏幕全屏的实现代码

Android 倒影算法的实现代码

编程语言 网络编程
Android 倒影算法的实现代码

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

区分JS中的undefined,null,"",0和false

区分JS中的undefined,null,"",0和false

Oracle PL/SQL入门案例实践

Oracle PL/SQL入门案例实践
下拉加载更多内容 ↓