Private voidgetlocationcity () {String serviceName=Context.location_service; Locationmanager=(Locationmanager) Getsystemservice (serviceName);//String Provider = Locationmanager.gps_provider;String Provider =Locationmanager.network_provider; Criteria Criteria=NewCriteria (); Criteria.setaccuracy (Criteria.accuracy_fine); Criteria.setaltituderequired (false); Criteria.setbearingrequired (false); Criteria.setcostallowed (true); Criteria.setpowerrequirement (criteria.power_low);//String Provider = Locationmanager.getbestprovider (criteria, true);
Locationmanager.requestlocationupdates (provider, N, locationlistener);
Location Location=locationmanager.getlastknownlocation (provider); GetAddress (location); } Private FinalLocationlistener Locationlistener =NewLocationlistener () { Public voidonlocationchanged (location) {getaddress); } Public voidonproviderdisabled (String provider) {getaddress (NULL); } Public voidonproviderenabled (String provider) {} Public voidOnstatuschanged (String provider,intstatus, Bundle extras) { } }; Private voidgetaddress (location location) {if(Location! =NULL) { Newmyasynccity (). Execute (location); } Else{log.d (TAG,"Updatewithnewlocation:"); } } Private classMyasynccityextendsAsynctask<location, Void, string>{@OverrideprotectedString doinbackground (location ... params) {List<Address> addlist =NULL; Geocoder GE=NewGeocoder (welcomeactivity. This); Try{addlist= Ge.getfromlocation (Params[0].getlatitude (), Params[0].getlongitude (), 1); } Catch(IOException e) {e.printstacktrace (); } returnAddlist.get (0). Getlocality (); } @Overrideprotected voidOnPostExecute (String city) {Super. OnPostExecute (city); Sputil.setlocationcity (city); } }
Android targeting templates