Android find GPS location once, show loading dialog

前端 未结 3 1840
感动是毒
感动是毒 2020-12-02 11:17

I am writing an app that requires the user\'s current location (lastknownlocation won\'t be very helpful) and displays a list of all the closest \"items\" to them taken from

3条回答
  •  执念已碎
    2020-12-02 11:59

    I still don't like how this works at all and it seems way more complicated than it should be but until someone provides a better way I have it working for now...

    If someone comes across this and suggest a much better cleaner result to just get the user's current location coordinates just once then stop looking that would be greatly appreciated!!

    private LocationControl locationControlTask;
    private boolean hasLocation = false;
    LocationHelper locHelper;
    

    From onCreate() I call

    locHelper = new LocationHelper();
    locHelper.getLocation(context, locationResult);
    locationControlTask = new LocationControl();
    locationControlTask.execute(this);
    

    I then have a a private class LocationControl

    private class LocationControl extends AsyncTask
        {
            private final ProgressDialog dialog = new ProgressDialog(activityname.this);
    
            protected void onPreExecute()
            {
                this.dialog.setMessage("Searching");
                this.dialog.show();
            }
    
            protected Void doInBackground(Context... params)
            {
                //Wait 10 seconds to see if we can get a location from either network or GPS, otherwise stop
                Long t = Calendar.getInstance().getTimeInMillis();
                while (!hasLocation && Calendar.getInstance().getTimeInMillis() - t < 15000) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                };
                return null;
            }
    
            protected void onPostExecute(final Void unused)
            {
                if(this.dialog.isShowing())
                {
                    this.dialog.dismiss();
                }
    
                if (currentLocation != null)
                {
                    useLocation();
                }
                else
                {
                    //Couldn't find location, do something like show an alert dialog
                }
            }
        }
    

    Then a locationResult..

    public LocationResult locationResult = new LocationResult()
        {
            @Override
            public void gotLocation(final Location location)
            {
                currentLocation = new Location(location);
                hasLocation = true;
            }
        };
    

    Also to avoid you pulling your hair out like I did for ages remember to stop the locationcontrol task if someone leaves the activity early and also stop location updates to stop the GPS from running when the user leaves. In onStop() do something like this:

    locHelper.stopLocationUpdates();
    locationControlTask.cancel(true);
    

    Also stopLocationUpdates in the Location Helper class does the following:

    public void stopLocationUpdates()
    {
    locationManager.removeUpdates(locationListenerGps);
    locationManager.removeUpdates(locationListenerNetwork);
    }
    

    Good luck with it, I needed it...

提交回复
热议问题