Code:
1 /*2 * Get the current location and send the location to the specified number via SMS3 */4 Public classLocationserviceextendsService {5 6 PrivateLocationmanager LM;7 PrivateMylocationlistener Listener;8 9 @OverrideTen Publicibinder onbind (Intent Intent) { One return NULL; A } - - @Override the Public voidonCreate () { - Super. OnCreate (); - -LM =(Locationmanager) Getsystemservice (location_service); +Listener =NewMylocationlistener (); -Criteria =NewCriteria (); +Criteria.setcostallowed (true);//are you allowed to use paid ACriteria.setaccuracy (Criteria.accuracy_fine);//get the precision of the position atString Provider = Lm.getbestprovider (criteria,true); -Lm.requestlocationupdates (provider, 0, 0, listener); - } - - classMylocationlistenerImplementsLocationlistener { - //get latitude and longitude when position changes in @Override - Public voidonlocationchanged (location location) { toString j = "Jingdu:" +location.getlongitude (); +String w = "Weidu:" +location.getlatitude (); -Sharedpreferences mpref = getsharedpreferences ("config", the mode_private); *Mpref.edit (). Putstring ("Location", J + ";" +W). commit (); $String Savedsafenumber = mpref.getstring ("Safenumber", "" ");Panax Notoginseng - Double[] locationdouble=Locationutils.standardtochina (Location.getlongitude (), Location.getlatitude ()); theSmsmanager.getdefault (). Sendtextmessage (Savedsafenumber,NULL, +"Phone ' s Location:" + locationdouble[0] + ";" + locationdouble[1],NULL,NULL);//The text of the reply is changed from Chinese to English and Chinese is garbled on the simulator. AStopself ();//after getting to latitude and longitude, stop the service the } + - //when the state changes $ @Override $ Public voidOnstatuschanged (String provider,intstatus, Bundle extras) { - - } the - //when the provider can use theWuyi @Override the Public voidonproviderenabled (String provider) { - Wu } - About //when the provider is not available $ @Override - Public voidonproviderdisabled (String provider) { - - } A + } the - @Override $ Public voidOnDestroy () { the Super. OnDestroy (); theLm.removeupdates (listener);//Stop all location services the } the -}
Locationmanager and targeting of Android development