Android基站定位原理及實(shí)現(xiàn)代碼
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("基站+聯(lián)網(wǎng)+google數(shù)據(jù)庫(kù)定位");
mainView=new LinearLayout(this);
mainView.setOrientation(LinearLayout.VERTICAL);
button=new Button(this);
button.setText("定位測(cè)試");
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("聯(lián)網(wǎng)成功\n");
br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
}else{
sb.append("聯(lián)網(wǎng)獲取數(shù)據(jù)失敗!\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);
}
}
}
所需權(quán)限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
- android 定位的4種方式介紹
- 在Android里完美實(shí)現(xiàn)基站和WIFI定位
- Android GPS定位測(cè)試(附效果圖和示例)
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
- Android實(shí)現(xiàn)GPS定位代碼實(shí)例
- Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- 基于Android實(shí)現(xiàn)百度地圖定位過程詳解
- Android實(shí)現(xiàn)地理定位功能
相關(guān)文章
Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間(示例代碼)
這篇文章主要介紹了Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間的示例代碼,需要的朋友可以參考下2017-05-05Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android使用 Retrofit 2.X 上傳多文件和多表單示例
本篇文章主要介紹了Android使用 Retrofit 2.X 上傳多文件和多表單示例,具有一定的參考價(jià)值,有興趣的小伙伴一起來了解一下2017-08-08Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法
這篇文章主要介紹了Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法,詳細(xì)分析了Android針對(duì)圖片的讀取、縮放及保存等操作技巧,需要的朋友可以參考下2016-06-06Android 通過httppost上傳文本文件到服務(wù)器的實(shí)例代碼
這篇文章主要介紹了Android 通過httppost上傳文本文件到服務(wù)器的實(shí)例代碼,非常簡(jiǎn)單易懂,非常實(shí)用,需要的朋友可以參考下2016-08-08Input系統(tǒng)按鍵事件的分發(fā)處理示例詳解
這篇文章主要為大家介紹了Input系統(tǒng)按鍵事件的分發(fā)處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09android實(shí)現(xiàn)簡(jiǎn)單底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見問題
這篇文章主要介紹了解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Android編程獲取并設(shè)置Activity亮度的方法
這篇文章主要介紹了Android編程獲取并設(shè)置Activity亮度的方法,涉及Android針對(duì)屏幕亮度的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12