android - Toggle Service via ToggleButton for GPS Tracker -
I am currently writing an app that should be on the following:
-
UI has only one toggle button if this GPS state is turned on, then it will be sent to an external server. If it stops, then nothing will happen.
-
If the button is on and the app turns off, the location should still be sent until the button is closed again.
How can I get this? I have read many threads and tutorials and dev.google.com, but I was not able to get the best solution to my problem.
My current perspective:
The main activity Java
on public void (see V) {if (OnOffButtonkisChecked ()) {Intent startIntent = new Intent (this, LocationService.class); StartService (startIntent); } And {stopService (new intent (this, LocationService.class)); Public service provides expanded service service service {final public static string START_ACTION = "START_LOCATION"
LocationService.java
; Last public static int NOTE_ID = 1; Private intuperate; Personal Location Manager Location Manager; Notify the private notification manager; @Ivarried Public Eye on On Bind (intro ARG0) {return tap; } @ Override Public Wide OnStart (Intent Intent, Int StartID) {Super .OnStart (Intentional, Initial); // show popup message toast.makeText (this, getText (R.string.start_message), toast.lNGTH_SHORT). Show (); // display icon requestLocationUpdates () in the status bar; } Private Zero Request Location Update () {if (locationManager! = Null) locationManager.removeUpdates (this); // Get location service criteria criteria = new criteria (); Crit.setPowerRequirement (Criteria.ACCURACY_FINE); String Best Providers = getLocationManager (). GetBestProvider (criticism, true); GetLocationManager (). RequestLocationUpdates (best provider, updateRate * 1000, 0 / * minDist * /, this); LocationService.running = true; } @ Override Public Empty OnDesto () {Super. Onsteroy (); GetLocationManager () removeUpdates (this). NotifyManager.cancel (NOTE_ID); Toast. Make Text (this, GetText (R.string.stop_message), Toast .LENGTH_SHORT). Show (); LocationService.running = false; } @Override public void onLocationChanged (Location) {// message for server} @Override public void onProviderDisabled (String provider) informed {// TODO stop service, user} @Override public void onProviderEnabled (String provider) {RequestLocationUpdates (); @Override} public void onStatusChanged (String provider, int position, bundled extra) informed {// TODO user} private LocationManager getLocationManager () {if (this.locationManager == NULL) this.locationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE); This return.locationManager; }}
I should be replaced with a much older GPS Tracker I get it found on the Internet (I know onStart () has been deleted and onCommandStart ()) I just want to know if general attitude is good ..
Regards. Is
approach looks fine to apply methods of call-back to report their location is required.
Comments
Post a Comment