The function of the program is to locate your current location through WiFi.
The main reference article is: http://www.cnblogs.com/rayee/archive/2012/02/02/2336101.html
It is clearly written, and the final effect is better than that on Android develop.
However, the Code still has some minor issues. After the original program clicks the button, progressdialog cannot be displayed. Add
A textview is added to display the longitude and latitude.
Post Code
Launcheractivity. Java
package com.android.demo;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class LauncherActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.launcher);Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubIntent goIntent = new Intent();goIntent.setClass(LauncherActivity.this, DemoActivity.class);startActivity(goIntent);}}, 3*1000);}}
Demoactivity. Java
Package COM. android. demo; import Java. io. bufferedreader; import Java. io. inputstreamreader; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. methods. httpget; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. entity. stringentity; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. JS On. jsonarray; import Org. JSON. jsonobject; import android. app. activity; import android. app. progressdialog; import android. content. context; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. telephony. telephonymanager; import android. telephony. GSM. gsmcelllocation; import android. util. log; import android. view. view; import android. widget. button; Import android. widget. textview; import android. widget. toast; import android. view. view. onclicklistener; public class demoactivity extends activity {private myhandler; private scell cell; private progressdialog mprogressdialog;/** obtain the longitude and latitude based on the base station data */private situde itude; /** obtain the geographic location */private string location;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedins Tancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);/** bind event to button */button btngetlocation = (button) findviewbyid (R. id. button1); btngetlocation. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {// todo auto-generated method stubonbtnclick ();}});} /** Base Station Information Structure */public class scell {public int MCC; Public int MNC; Public int LAC; Public int CID ;}/** Longitude and latitude information struct */public class situde {Public String latitude; Public String longpolling;}/** click the callback function */private void onbtnclick () {/** pop up a waiting box */myhandler = new myhandler (); toast. maketext (getapplicationcontext (), "ha", toast. length_short ). show (); mprogressdialog = new progressdialog (this); mprogressdialog. setmessage ("getting... "); mprogressdialog. setprogressstyle (progressdialog. style_spinner); mprogressdialog. sh Ow (); mythread M = new mythread (); New thread (m ). start ();}/*** get Base Station Information ** @ throws exception */private scell getcellinfo () throws exception {scell cell = new scell (); /** call the API to obtain the base station information */telephonymanager mtelnet = (telephonymanager) getsystemservice (context. telephony_service); gsmcelllocation location = (gsmcelllocation) mtelnet. getcelllocation (); If (location = NULL) throw new exception ("failed to get the base station information"); Str Ing operator = mtelnet. getnetworkoperator (); int MCC = integer. parseint (operator. substring (0, 3); int MNC = integer. parseint (operator. substring (3); int cid = location. getcid (); int LAC = location. getlac ();/** put the obtained data in the struct */cell. MCC = MCC; cell. MNC = MNC; cell. LAC = LAC; cell. cid = CID; return cell;}/*** obtain latitude and longitude ** @ throws exception */private situde getitude (scell cell) throws exception {situde itude = New sent ();/** use the default httpclient of Android */httpclient client = new defaulthttpclient (); /** use the post Method */httppost post = new httppost ("http://www.google.com/loc/json"); try {/** to construct the JSON data of post */jsonobject holder = new jsonobject (); holder. put ("version", "1.1.0"); holder. put ("host", "maps.google.com"); holder. put ("address_language", "zh_cn"); holder. put ("request_address", true); holder. put ("radio_type", "GSM "); Holder. put ("carrier", "HTC"); jsonobject Tower = new jsonobject (); tower. put ("mobile_country_code", cell. MCC); tower. put ("mobile_network_code", cell. MNC); tower. put ("cell_id", cell. CID); tower. put ("location_area_code", cell. LAC); jsonarray towerarray = new jsonarray (); towerarray. put (Tower); holder. put ("cell_towers", towerarray); stringentity query = new stringentity (holder. tostring (); Post. setentity (Qu Ery);/** send post data and obtain returned data */httpresponse response = client.exe cute (post); httpentity entity = response. getentity (); bufferedreader buffreader = new bufferedreader (New inputstreamreader (entity. getcontent (); stringbuffer strbuff = new stringbuffer (); string result = NULL; while (result = buffreader. readline ())! = NULL) {strbuff. append (result);}/** obtain the longitude and latitude of the JSON data returned by parsing */jsonobject JSON = new jsonobject (strbuff. tostring (); jsonobject subjosn = new jsonobject (JSON. getstring ("location"); locate. latitude = subjosn. getstring ("latitude"); longitude. longpolling = subjosn. getstring ("longpolling"); log. I ("success", fail. latitude + longitude. longpolling);} catch (exception e) {log. E (E. getmessage (), E. tostring (); throw new exception ("Get Latitude Error: "+ E. getmessage ();} finally {post. abort (); client = NULL;} return region;}/*** obtain the geographic location ** @ throws exception */private string getlocation (situde location) throws exception {string resultstring = "";/** the get method is used here to directly add the parameter to the URL */string urlstring = string. format ("http://maps.google.cn/maps/geo? Key = abcdefg & Q = % s, % s ", required. latitude, longitude. longpolling); log. I ("url", urlstring);/** create httpclient */httpclient client = new defaulthttpclient (); /** use the get Method */httpget get = new httpget (urlstring); try {/** to initiate a GET request and obtain the returned data */httpresponse response = client.exe cute (get ); httpentity entity = response. getentity (); bufferedreader buffreader = new bufferedreader (New inputstreamreader (entity. getcontent (); string Buffer strbuff = new stringbuffer (); string result = NULL; while (result = buffreader. Readline ())! = NULL) {strbuff. append (result);} resultstring = strbuff. tostring ();/** parse JSON data to obtain the physical address */If (resultstring! = NULL & resultstring. length ()> 0) {jsonobject = new jsonobject (resultstring); jsonarray = new jsonarray (jsonobject. get ("placemark "). tostring (); resultstring = ""; for (INT I = 0; I <jsonarray. length (); I ++) {resultstring = jsonarray. getjsonobject (I ). getstring ("Address") ;}} catch (exception e) {Throw new exception ("An error occurred while obtaining the physical location:" + E. getmessage ();} finally {Get. abort (); client = NULL;} return resultstring;}/** display result */private void showresult (scell cell, string location, s0000margin) {textview celltext = (textview) findviewbyid (R. id. celltext); celltext. settext (string. format ("base station information: MCC: % d, MNC: % d, LAC: % d, CID: % d", cell. MCC, cell. MNC, cell. LAC, cell. CID); textview locationtext = (textview) findviewbyid (R. id. lacationtext); locationtext. settext ("physical location:" + location); textview itudetext = (textview) findviewbyid (R. id. itudetext); itudetext. settext ("longitude:" + longitude. latitude + "latitude:" + latitude. longpolling);} class myhandler extends handler {public myhandler () {} public myhandler (low.l) {super (l);} // The subclass must override this method, take over data @ override public void handlemessage (Message MSG) {// todo auto-generated method stub log. D ("myhandler", "handlemessage ...... ");/** display result */mprogressdialog. dismiss (); If (MSG. arg1 = 1) {showresult (cell, location, location);} super. handlemessage (MSG); // UI can be updated here} class mythread implements runnable {public void run () {message MSG = new message (); MSG. arg1 = 1; try {/** obtain Base Station Data */cell = getcellinfo ();/** obtain longitude and latitude Based on base station data */longitude = getitude (cell ); /** get geographic location */location = getlocation (location);/** close dialog box */MSG. arg1 = 1; myhandler. sendmessage (MSG); // send a message to handler, update UI} catch (interruptedexception E) {// todo auto-generated Catch Block/** Display Error */textview celltext = (textview) findviewbyid (R. id. celltext); celltext. settext (E. getmessage (); log. E ("error", E. getmessage ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} log. D ("thread ....... "," mthread ........ ");}}}