The customer needs to track the waybill information and decide to use GPS and base station positioning. After several days of hard work, we have achieved remarkable results. The following is the sample code:
Locationmanager = (locationmanager) getsystemservice (context. location_service); // create the locationmanager object provider = locationmanager. getbestprovider (getcriteria (), true); // sets the query condition and returns the positioning method. The default value is GPS positioning. // sets the listener locationlistener = new locationlistener () {@ override public void onstatuschanged (string provider, int status, bundle extras) {// todo auto-generated method stub} @ override public void onproviderenable D (string provider) {// todo auto-generated method stub location L = locationmanager. getlastknownlocation (provider); // obtain the location information updateview (l); // update the edittext control content} @ override public void onproviderdisabled (string provider) {updateview (null );} @ override public void onlocationchanged (location) {// todo auto-generated method stub updateview (location) ;}}; BTN. setonclicklistener (New onclickl Istener () {@ override public void onclick (view v) {// todo auto-generated method stub progressdialog = new progressdialog (demoactivity. this); progressdialog. setprogressstyle (progressdialog. style_spinner); progressdialog. setmessage ("locating! "); Progressdialog. show (); New thread () {@ override public void run () {getlocation ();};}. start () ;}}); Private void getlocation () {// get Location Management Service try {location = locationmanager. getlastknownlocation (provider); // obtain the location address = GPS through GPS. updatetonewlocation (location); handler. sendemptymessage (2);} catch (exception e) {// todo auto-generated catch block address = E. getmessage (); handler. sendemptymessage (3) ;}} private handler = new handler () {@ override public void handlemessage (Message MSG) {Switch (MSG. what) {Case 0: // log on to load the break; Case 1: break; Case 2: progressdialog. dismiss (); updateview (location); default: Break ;}};};
Note:
1. Rewrite onstart to check whether GPS is enabled
View code
1 @Override 2 protected void onStart() { 3 // TODO Auto-generated method stub 4 super.onStart(); 5 6 Boolean a = locationManager 7 .isProviderEnabled(LocationManager.GPS_PROVIDER); 8 9 if (!a) {10 Intent intent = new Intent();11 intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);12 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);13 14 try {15 startActivity(intent);16 17 } catch (ActivityNotFoundException ex) {18 19 // The Android SDK doc says that the location settings20 // activity21 // may not be found. In that case show the general settings.22 23 // General settings activity24 intent.setAction(Settings.ACTION_SETTINGS);25 try {26 startActivity(intent);27 } catch (Exception e) {28 }29 }30 }31 32 }
2. Rewrite onresume and set the listener.
View code
1 @ override2 protected void onresume () {3 // todo auto-generated method stub4 super. onresume (); 5 6 locationmanager. requestlocationupdates (provider, 1000, locationlistener); // the actual location is 8 9 in network mode}
2. Rewrite onpause and log out of listening
View code
1 @Override2 protected void onPause() {3 // TODO Auto-generated method stub4 super.onPause();5 6 if (locationManager != null) {7 locationManager.removeUpdates(locationListener);8 }9 }