Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:28:33+00:00 2026-05-23T11:28:33+00:00

I’m beta testing my first Android app and have had a few users mention

  • 0

I’m beta testing my first Android app and have had a few users mention that when they attempt to lookup by GPS it hangs. In order to improve error handling around this I wanted to get the opinion of people who have apps in the wild.

My current activity does the following to kick off the lookup

findViewById(R.id.gpsButton).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    MyLocationListener mlocListener = new MyLocationListener();

                    Criteria locationCriteria = new Criteria();
                    locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);

                    mlocManager.requestLocationUpdates(mlocManager.getBestProvider(locationCriteria, true), 0, 0, mlocListener);
                }
            });

The implementation of my custom location lookup class is below

public class MyLocationListener implements LocationListener {
        private boolean alreadyLocatedDevice;
        private ProgressDialog dialog;

        public MyLocationListener() {
            this.dialog = ProgressDialog.show(LocationLookup.this, "", "Loading...");
        }

        @Override
        public void onProviderDisabled(String provider) {
            this.dialog.dismiss();
            DialogHelper.showDialogWithMessageAndTitle("", "You don't currently have location services turned on", LocationLookup.this);
        }

        @Override
        public void onLocationChanged(android.location.Location location) {
            if (!alreadyLocatedDevice) {
                alreadyLocatedDevice = true;
                Location loc = new Location();
                loc.setLng(Double.toString(location.getLongitude()));
                loc.setLat(Double.toString(location.getLatitude()));

                ((AppDelegate) getApplicationContext()).setSelectedLocation(loc);
                Intent findKioskLocation = new Intent(LocationLookup.this, FindKioskLocation.class);

                this.dialog.dismiss();

                startActivity(findKioskLocation);
            }
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void onProviderEnabled(String s) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }

And finally I’ve added both the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in my manifest file.

Any help would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:28:34+00:00Added an answer on May 23, 2026 at 11:28 am

    For anyone who might follow this thread -I found a mixture of my own approach (admittedly hackish in this code example) and the one mentioned by @bear to work without any issues (plus the location lookup was fast/accurate and error free)

    I found the example listed by @bear to be a little more complex than I needed. For starters I wanted to kick off the GPS lookup when a button was clicked and have a simple async task wrapping this so it would throw up a dialog/etc

    Next I wanted the exact latitude and longitude (no need to pass this off to another class because my example was simply to use the lat + lng to locate a resource and plot it)

    So if you can follow my untested rather copy/paste approach here goes…

    Inside your activity you would spin up the service during an onclick lets say …

    LocationManager networkManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationManager gpsManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
    LocationService locationProcessor = new LocationService(YourActivityName.this, networkManager, gpsManager, dialog);
    locationProcessor.onStartCommand();
    

    Now the location service itself

    package com.epicsoftware.android.global;
    
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.Handler;
    import com.epicsoftware.android.activity.LocationLookup;
    
    public class LocationService {
        private LocationManager networkLm;
        private LocationManager gpsLm;
        private LocationListener networkListener;
        private LocationListener gpsListener;
    
        private boolean isRunning;
        private boolean networkLocDisabled;
        private boolean gpsLocDisabled;
        private Context activity;
        private LocationManager tmpNetworkManager;
        private LocationManager tmpGpsManager;
        private Handler locationHandler;
        private ProgressDialog dialog;
        private boolean gpsUpdated;
        private boolean done;
    
        public LocationService(final Context activity, LocationManager networkManager, LocationManager gpsManager, ProgressDialog dialog) {
            this.tmpNetworkManager = networkManager;
            this.tmpGpsManager = gpsManager;
            this.activity = activity;
            this.dialog = dialog;
        }
    
        public void onStartCommand() {
            if (!isRunning) {
                isRunning = true;
    
                startLocationListeners();
    
                locationHandler = new Handler();
                getLocationByZip.start();
            }
        }
    
        private void startLocationListeners() {
            networkListener = new NetworkLocationListener();
            gpsListener = new GpsLocationListener();
    
            networkLm = tmpNetworkManager;
            gpsLm = tmpGpsManager;
    
            networkLm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
            gpsLm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
        }
    
    
        private Thread getLocationByZip = new Thread() {
            public void run() {
                try {
                    for (int i = 0; i < 15;) {
                        if (!locationDisabled() || !gpsUpdated) {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                break;
                            }
                            i++;
                        } else {
                            break;
                        }
                    }
    
                    locationHandler.post(monitorTheNetworkAndGpsProviders);
                } catch (Exception e) {
                    killService();
                    done = true;
                }
            }
        };
    
        private Runnable monitorTheNetworkAndGpsProviders = new Runnable() {
            @Override
            public void run() {
                killService();
                dialog.dismiss();
                if (!done) {
                    done = true;
                    ((LocationLookup) activity).warnUserThatLocationServicesAreDisabledOrFailed();
                }
            }
        };
    
        private boolean locationDisabled() {
            if (gpsLocDisabled && networkLocDisabled) {
                done = true;
                ((LocationLookup) activity).warnUserThatLocationServicesAreDisabledOrFailed();
    
                return true;
            } else {
                return false;
            }
        }
    
        private void updateDb(Double lat, Double lon) {
            done = true;
            ((LocationLookup) activity).setLocationDataAndSpinUpNextActivity(lat, lon);
        }
    
        public void killService() {
            networkLm.removeUpdates(networkListener);
            gpsLm.removeUpdates(gpsListener);
        }
    
        public class NetworkLocationListener implements LocationListener {
            @Override
            public void onLocationChanged(Location location) {
                if (location != null) {
                    updateDb(location.getLatitude(), location.getLongitude());
                }
            }
    
            @Override
            public void onProviderDisabled(String provider) {
                networkLocDisabled = true;
            }
    
            @Override
            public void onProviderEnabled(String provider) {
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }
    
        public class GpsLocationListener implements LocationListener {
            @Override
            public void onLocationChanged(Location location) {
                if (location != null) {
                    gpsUpdated = true;
                    updateDb(location.getLatitude(), location.getLongitude());
                }
            }
    
            @Override
            public void onProviderDisabled(String provider) {
                gpsLocDisabled = true;
            }
    
            @Override
            public void onProviderEnabled(String provider) {
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.