Package CN. myandroid. test; import Java. io. bufferedreader; import Java. io. inputstreamreader; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; 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. con Tent. context; import android. OS. bundle; import android. telephony. telephonymanager; import android. telephony. GSM. gsmcelllocation; import android. view. view; import android. widget. button; import android. widget. textview; public class mylocation extends activity {textview mtextview; button mbutton; telephonymanager TM;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedins Tancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtextview = (textview) findviewbyid (R. id. textview01); mbutton = (button) findviewbyid (R. id. button01); TM = (telephonymanager) This. getsystemservice (context. telephony_service); mbutton. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubgsmcelllocation GCL = (Gsmcelllocation) TM. getcelllocation (); int cid = GCL. getcid (); int LAC = GCL. getlac (); int MCC = integer. valueof (TM. getnetworkoperator (). substring (0, 3); int MNC = integer. valueof (TM. getnetworkoperator (). substring (3, 5); try {// assemble JSON query string jsonobject holder = new jsonobject (); holder. put ("version", "1.1.0"); holder. put ("host", "maps.google.com"); // holder. put ("address_language", "zh_cn"); holder. put ("re Quest_address ", true); jsonarray array = new jsonarray (); jsonobject DATA = new jsonobject (); data. put ("cell_id", CID); // 25070data. put ("location_area_code", LAC); // 4474data. put ("mobile_country_code", MCC); // 460data. put ("mobile_network_code", MNC); // 0array. put (data); holder. put ("cell_towers", array); // creates a connection, sends a request, and accepts the response defaulthttpclient client = new defaulthttpclient (); httppost post = new httppost ("H Ttp: // www.google.com/loc/json "); stringentity Se = new stringentity (holder. tostring (); Post. setentity (SE); httpresponse resp = client.exe cute (post); httpentity entity = resp. getentity (); bufferedreader BR = new bufferedreader (New inputstreamreader (entity. getcontent (); stringbuffer sb = new stringbuffer (); string result = BR. readline (); While (result! = NULL) {sb. append (result); Result = BR. readline ();} mtextview. settext (sb. tostring ();} catch (exception e) {// todo: handle exception }}});}}
Note that you must add the following permissions to the activity: <uses-Permission Android: Name = "android. Permission. permission_name"
/>
<Uses-Permission Android: Name = "android. Permission. access_coarse_location"/>
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
<Uses-Permission Android: Name = "android. Permission. Internet"/>
For the base station query protocol, please refer to this link: http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html