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

  • Home
  • SEARCH
  • 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 9134061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:35:40+00:00 2026-06-17T08:35:40+00:00

Working on some check-in functionality for an application, and trying to figure out the

  • 0

Working on some check-in functionality for an application, and trying to figure out the cleanest way to handle the lastKnowLocation being null. Currently, I attach a location listener and then fire off an asynctask which publishes an indeterminate progressdialog, alerting the user that I am searching for a the gps to gather their location; the doInBackground method is essentially just a busy wait loop, waiting on the location to not be null. As a disclaimer, I feel like this current implementation is bit “jenky” from a code standpoint, but provides the expected user experience. However, I am wondering if anybody could provide insight as to what would be a better way to handle this fairly common scenario.

Thanks

  • 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-06-17T08:35:41+00:00Added an answer on June 17, 2026 at 8:35 am

    I’m not for the progress dialog because if the GPS takes forever then so will your app, killing the UX. In the activity with the dialog why not have a global locationFound boolean, a lastLongitude and lastLatitude. The advantage of that being if the you use the GPS returned to open another activity and the user navigates backwards you can even implement a “See previous results” or whatever the terminology is for your app. Here’s how i implemented a GPS location wait in an activity for a small app i did a while ago, excluding the code that doesn’t matter for your question

    public class MainActivity extends Activity {
            TextView txt;
            Button btnGPS;
            Button btnSeeLast;
            LocationManager locationManager;
            String provider;
            boolean gotLoc;
            boolean hasLastLoc;
            double lastLat;
            double lastLon;
            LocationListener myLocationListener;
    
    
        @Override
    public void onDestroy(){
        super.onDestroy();
        locationManager.removeUpdates(myLocationListener);  
    
    }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            .....
            txt = (TextView)findViewById(R.id.tekst);
            txt.setText("Waiting for GPS location");
            btnSeeLast = (Button)findViewById(R.id.btnSeeLast);
            btnSeeLast.setEnabled(false);
            hasLastLoc = false;
            String context = Context.LOCATION_SERVICE;
            gotLoc = false;
            locationManager = (LocationManager)getSystemService(context);
            provider = LocationManager.GPS_PROVIDER;
            myLocationListener = new UpdatesReceiver();
            int t = 5000;
            int distance = 5;
            locationManager.requestLocationUpdates(provider, t, distance, myLocationListener);
            checkGPS();
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null){
            gotLoc = true;
            updateWithNewLocation(location, true);
            }
          }
    
    private void checkGPS(){
        if (!locationManager.isProviderEnabled(provider)){
            buildAlertMessageNoGps();
            }   
    }
    
    public void btnSeeLastClick(View view){
        Intent intent = new Intent(this, NextActivity.class);
        intent.putExtra("latitude", String.valueOf(lastLat));
        intent.putExtra("longitude", String.valueOf(lastLon));
        intent.putExtra("prev", false);     
        startActivity(intent);
    
    }
    
    
    private void updateWithNewLocation(Location location, boolean prev){
        btnGPS.setEnabled(true);
        String latLongString;
    
        if (location != null){
            double lat = location.getLatitude();
            double lng = location.getLongitude();
    
            //remember
            lastLat = lat;
        lastLon = lng;
        hasLastLoc = true;
        btnSeeLast.setEnabled(true);
            //end remember
            latLongString = "Latitude: " + lat + "\nLongitude: " + lng;
            txt.setText("");
            //txt.setText(latLongString);return;
    
            Intent intent = new Intent(this, AgentsList.class);
            intent.putExtra("latitude", String.valueOf(lat));
            intent.putExtra("longitude", String.valueOf(lng));
    
            if (prev){
                intent.putExtra("prev", true);              
            }
            else{
                intent.putExtra("prev", false);
            }
            startActivity(intent);
    
            }
        else{
            latLongString = "Failed to get a response from your GPS. Try again";
            txt.setText(latLongString);
            }
    
    
    
    }
    
    private class UpdatesReceiver implements LocationListener{
        @Override
        public void onLocationChanged(Location location) {
            if (gotLoc == false){
                gotLoc = true;
                updateWithNewLocation(location, false);
    
    
            }           
    
        }
    
        @Override
        public void onProviderDisabled(String provider){
        // Update application if provider disabled.
        }
    
        @Override
        public void onProviderEnabled(String provider){
        // Update application if provider enabled.
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){
        // Update application if provider hardware status changed.
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a jQuery functionality to check a textbox for certain
I'm trying to figure out a way to add a self-update feature to a
Guys check this website on FF and Chrome For some reason(s), jQuery isn't working
I`m working on some sql queries to get some data out of a table;
I'm trying to create some tests for an application that uses Spring and JPA
I am currently working on converting an existing webforms application to MVC and am
I'm trying to create some functionality that allows me to arbitrarily show a collection
I am working on app that check some of the status of phone every
i working on a website and i have to check some form such that
I am working with some custom ajax functionality using Telerik's MVC Grid and I

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.