Android base station positioning

Source: Internet
Author: User

Here we will share with you the implementation of the base station positioning. The base station positioning must first obtain the signal information of the mobile phone through telephonymanager, such as the Country Code of the base station and the residential ID ...... after obtaining these parameters, You need to submit these parameters to the interface provided by Google, and then return the information about the base station.

Let's talk a little bit about the Code:

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 cel1_infomanager implements serializable {/*****/Private Static final long serialversionuid = 5481154371450408380l; private context; public celemediinfomanager (context) {super (); this. context = context;} public list <cellinfo> getcellinfo () {list <cellinfo> listinfo = new arraylist <cellinfo> (); 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 <neighboringcellinfo> 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 {// base station number public int cellid; // country code public int mobilecountrycode; // network code public int mobilenetworkcode; // region code public int locationareacode; Public String radio_type; Public cellinfo () {super ();}}}

The above figure shows the mobile phone signal. The following figure shows how to send the information to the Google server and parse the result:

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.Serializable;import java.util.List;import org.apache.http.HttpEntity;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.location.Location;import android.util.Log;import com.metarnet.gps.CellIdInfoManager.CellInfo;/** * @author Administrator *  */public class NetworkLocationManager implements Serializable {/** *  */private static final long serialVersionUID = 1185788569820321281L;public static Location getBaseStationLocation(List<CellInfo> cellID) {if (cellID == null) {Log.i("TAG", "cellId is null.");return null;}DefaultHttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost("http://www.google.com/loc/json");JSONObject holder = new JSONObject();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 = new JSONArray();current_data = new JSONObject();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 (int i = 1; i < cellID.size(); i++) {data = new JSONObject();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 = new StringEntity(holder.toString());post.setEntity(se);HttpResponse resp = client.execute(post);int state = resp.getStatusLine().getStatusCode();if (state == HttpStatus.SC_OK) {HttpEntity entity = resp.getEntity();if (entity != null) {BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));StringBuffer sb = new StringBuffer();String resute = "";while ((resute = br.readLine()) != null) {sb.append(resute);}br.close();data = new JSONObject(sb.toString());data = (JSONObject) data.get("location");Location loc = new Location(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());return loc;} else {return null;}} else {Log.v("TAG", state + "");return null;}} catch (Exception e) {Log.e("TAG", e.getMessage());return null;}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.