Android's Locationmanager
Last Update:2016-01-25
Source: Internet
Author: User
<span id="Label3"></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Location Services is one of the most commonly used services on mobile devices, and it also provides such core services on Android Systems. Its API is mainly under the Android.location package, specifically as Follows:</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘;"></span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘;">In the above we can see the location services involved in the Android platform api, now for a few commonly used classes and interfaces to do a simple description.</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘;">1. Locationmanager: core class for acquiring and invoking location Services.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">2, Locationprovider: really used to obtain location information components, mainly divided into two categories, a reliance on GPS devices, a dependency on the Network.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">3, Location: used to describe the address position information, including longitude, dimension, altitude, direction and other Information.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">4, Geocode: used to process the address of the Encoding.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">5. Criteria: used for querying and selecting locationprovider.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">6, Address: used to describe the location Information.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">7, Locationlistener: used to receive location information changes when the Listener.</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘;"> </span><span style="font-family: ‘Microsoft YaHei‘;">Locationmanager is the entire location service portal, and its instances are obtained by Context.getsystem (context.location_service), just like any other manager. The most important way to do this is to register and unregister the Service's overloaded methods:</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘;">1, Requestlocationupdates: Registration Service Monitoring</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">2, removeupdate: Cancel Service Monitoring</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">3, getallproviders: get all the Locationprovider</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘;">Locationlistener: used to receive notification of changes in location from locationmanager, when registering a service, calling Locationmanager's requestlocationupdates (String, long, float, locationlistener). The methods in the interface are as Follows:</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘;">1. Public abstract void <strong>onlocationchanged</strong> : This method is called when the position is Changed. The parameter location represents the new Position.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">2. Public abstract void <strong>onstatuschanged</strong> (String provider, int status, Bundle extras): This method is called when the state of the provider is changed, The status is available, unavailable, and Service-free.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">3, public abstract void <strong>onproviderdisabled</strong>(String provider): This method is called when provider is closed by the User.</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">4. Public abstract void <strong>onporviderenabled</strong> : This method is called when provider is opened by the User.</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘;"> </span><span style="font-family: ‘Microsoft YaHei‘;">The location class encapsulates positional information, including longitude, dimension, elevation, speed, time, and so on, which can be obtained by invoking the following method:</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘;">1, getlongitude (): Longitude</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">2, getlatitude (): Dimension</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">3, getaccuracy (): Accuracy</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">4, getspeed (): Speed</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘;">Geocoder is used to convert between street addresses and latitude and longitude, which provides methods for two geocoding functions: finding the latitude and longitude of an address and finding a street address for a given latitude and Longitude.</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘;">1, getfromlocation (double latitude,double longitude,int maxResults)</span></p> <p><span style="font-family: ‘Microsoft YaHei‘;">2, getfromlocationname (String locationname,int maxResults)</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘;">Below we demonstrate the specific use of location services through a case study</span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Locationmanageractivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">actionbaractivity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Locationmanager locationmanager; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Jredulocationlistener listener; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Geocoder Geocoder; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">TextView show; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">TextView locationshow; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_location_manager); Locationmanager</span></span>=<span style="color: #000000;"><span style="color: #000000;">(locationmanager) Getsystemservice (location_service); Show</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.show); Locationshow</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.showlocation); Listener</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jredulocationlistener (); Geocoder</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Geocoder (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">getlocationprovider (View v) {List</span></span><String> providers =<span style="color: #000000;"><span style="color: #000000;">locationmanager.getallproviders (); StringBuilder SB</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;">(String p:providers) {sb.append (p). Append (</span></span>"\ n"<span style="color: #000000;"><span style="color: #000000;">); } show.settext (sb.tostring ()); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">getLocation (View v) {criteria</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Criteria (); Criteria.setaccuracy (criteria.accuracy_fine); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set positioning accuracy</span></span>Criteria.setaltituderequired (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Altitude</span></span>Criteria.setbearingrequired (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span>Criteria.setcostallowed (<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #000000;"><span style="color: #000000;">criteria.setpowerrequirement (criteria.power_low); Criteria.setspeedrequired (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the right positioning method based on the above settings</span></span>String providername= Locationmanager.getbestprovider (criteria,<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Register</span></span>Locationmanager.requestlocationupdates (providername,0,0<span style="color: #000000;"><span style="color: #000000;">, listener); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Jredulocationlistener<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">locationlistener{</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* When the position is changed *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;"></span> location</span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onlocationchanged (location Location) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span>Latitude=location.getlatitude ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dimension of</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span>Longitude = Location.getlongitude ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Longitude</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder SB</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder (); Sb.append (</span></span>"longitude:"). Append (longitude). Append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>dimension:). Append (latitude). Append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"precision:"). append (location.getaccuracy ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"speed:"). append (location.getspeed ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"Bearing:"). append (location.getbearing ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"time:"). append (location.gettime ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Locationshow.settext (sb.tostring ()); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">initiate a child thread for geo-encoding resolution</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Thread (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{List</span></span><Address> addresses=<span style="color: #000000;"><span style="color: #000000;">geocoder.getfromlocation (latitude, longitude,</span></span>1<span style="color: #000000;"><span style="color: #000000;">); Message msg</span></span>= Handler.obtainmessage (1<span style="color: #000000;"><span style="color: #000000;">); Msg.obj</span></span>=<span style="color: #000000;"><span style="color: #000000;">addresses; Handler.sendmessage (msg); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); }}). Start (); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onstatuschanged (String s,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">i, Bundle Bundle) {} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onproviderenabled (String s) {} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onproviderdisabled (String s) {}}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Handler Handler =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Handler () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">handlemessage (Message Msg) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Handlemessage (msg); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(msg.what==1<span style="color: #000000;"><span style="color: #000000;">) {StringBuilder SB</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder (); List</span></span><Address> addresses = (list<address><span style="color: #000000;"><span style="color: #000000;">) msg.obj; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(addresses.size () >0<span style="color: #000000;"><span style="color: #000000;">) {address Address</span></span>= Addresses.get (0<span style="color: #000000;"><span style="color: #000000;">); StringBuilder sbaddress</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>StringBuilder ("Address:"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=0;i<address.getmaxaddresslineindex (); i++<span style="color: #000000;"><span style="color: #000000;">) {sbaddress.append (address.getaddressline (i)). Append (</span></span>"\ t"<span style="color: #000000;"><span style="color: #000000;">); } sbaddress.append (</span></span>"\ n"<span style="color: #000000;"><span style="color: #000000;">); Sbaddress.append (</span></span>"countryname:"). append (address.getcountryname ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sbaddress.append (</span></span>"countrycode:"). append (address.getcountrycode ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sbaddress.append (</span></span>"sublocality:"). append (address.getlocality ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); Sbaddress.append (</span></span>"sublocality:"). append (address.getsublocality ()). append ("\ n"<span style="color: #000000;"><span style="color: #000000;">); String</span> old</span>=<span style="color: #000000;"><span style="color: #000000;">locationshow.gettext (). toString (); Locationshow.settext (</span> old</span>+ "\ n" +<span style="color: #000000;"><span style="color: #000000;">sbaddress.tostring ()); } } } }; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnDestroy () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnDestroy (); Locationmanager.removeupdates (listener); }}</span></span></pre><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">The cases are as follows:</span></p></p><p align="left"><p align="left"></p></p><p><p></p></p>Jerry Education<br>Source:<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">http://www.cnblogs.com/jerehedu/</span></span></span><br><span style="font-family: ‘Microsoft YaHei‘; color: #000000;"><span style="font-family: ‘Microsoft YaHei‘; color: #000000;">Copyright Notice: The copyright of this article belongs to <span style="color: #000000;">cigarettes</span> </span></span><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">Desk</span></span><span style="color: #000000; font-family: ‘Microsoft YaHei‘;"><span style="color: #000000; font-family: ‘Microsoft YaHei‘;"><span style="color: #000000;">Jerry Education</span></span></span><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">Section</span></span><span style="font-family: ‘Microsoft YaHei‘; color: #000000;"><span style="font-family: ‘Microsoft YaHei‘; color: #000000;"><span style="color: #000000;">Technology co., Ltd.</span> and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.<br>Technical Consultation:</span></span><p><p>Android's Locationmanager</p></p></span>