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 8396803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:37:52+00:00 2026-06-09T20:37:52+00:00

I am building an application that uses GPS to determine users current country. Now,

  • 0

I am building an application that uses GPS to determine users current country. Now, since GPS takes a while to find the location, while it is searching i want to show a ProgressDialog.

So inside my LocationListener, I called a ProgressDialog that i declared before called

pd

And right underneath the part where it shows i created a thread to do the GPS stuff. for some reason, the ProgressDialog isn’t showing :/

Here is my code:

pd = ProgressDialog.show(TipCalculatorActivity.this, "", "Looking for GPS satellites...");
        new Thread() 
        {
          public void run() 
          {
             try
               {
                Geocoder geocoder = new Geocoder(TipCalculatorActivity.this, Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = geocoder.getFromLocation(lat, lng, 1);
                    countryCode = addresses.get(0).getAddressLine(2);
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
                    finish();
                }
                if(countryCode==null){
                    Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
                    finish();
                }
                Toast.makeText(TipCalculatorActivity.this, countryCode, Toast.LENGTH_LONG).show();

              }
            catch (Exception e)
            {
                Log.e("tag",e.getMessage());
            }
        // dismiss the progressdialog   
          pd.dismiss();
         }
        }.start();

What is wrong here?!

Thanks!

EDIT

My new code:
Where i call the AsyncTask:

    LocationListener onLocationChange=new LocationListener() {
    public void onLocationChanged(Location loc) {
        //sets and displays the lat/long when a location is provided
        lat = loc.getLatitude();
        lng = loc.getLongitude();

        MyAsyncTask task = new MyAsyncTask(TipCalculatorActivity.this, lng, lat);
        task.execute();
        Toast.makeText(TipCalculatorActivity.this, "Done :D", Toast.LENGTH_LONG).show();

    }

The AsyncTask:

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

private Activity activity;
private ProgressDialog progressDialog;

private double longitude;
private double latitude;
private String countryCode;

public MyAsyncTask(Activity activity, double longitude, double latitude) {
    super();
    this.activity = activity;
    this.longitude = longitude;
    this.latitude = latitude;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    ProgressDialog.show(activity, "", "Looking for GPS satellites...");
}

@Override
protected Result doInBackground(Void... v) {
    Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
    List<Address> addresses;
    try {
        addresses = geocoder.getFromLocation(latitude, longitude, 1);
        countryCode = addresses.get(0).getAddressLine(2);
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
    }
    if(countryCode==null){
        Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
    }
    Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show();
    return null;
}

@Override
protected void onPostExecute(Result result) {
    progressDialog.dismiss();
    Toast.makeText(activity.getApplicationContext(), "Finished.", 
    Toast.LENGTH_LONG).show();
}
}
  • 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-09T20:37:54+00:00Added an answer on June 9, 2026 at 8:37 pm

    If I were you, I will use an AsyncTask for that :

    public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
    
                            private Activity activity;
                            private ProgressDialog progressDialog;
    
                            private long longitude;
                            private long latitute;
    
                public MyAsyncTask(Activity activity, Long longitude, Long latitude) {
                                super();
                    this.activity = activity;
                    this.longitude = longitude;
                    this.latitude = latitude;
                }
    
                @Override
                protected void onPreExecute() {
                super.onPreExecute();
                    progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
                }
    
                @Override
                protected Result doInBackground(Void... v) {
                //do your stuff here
                return null;
    
                }
    
                @Override
                protected void onPostExecute(Result result) {
                    progressDialog.dismiss();
                    Toast.makeText(activity.getApplicationContext(), "Finished.", 
                            Toast.LENGTH_LONG).show();
                }
    
    
    }
    

    Call it from the activity:

    MyAsyncTask task = new AsyncTask(myActivity.this, lng, lat);
    task.execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a web application that uses the database for Users, Security/roles, and
I am building a web application that uses a lot of Javascript. Now I
I'm building an application that uses an elaborate API to fetch data from Youtube,
I am building an android application that uses several third party libraries. I have
I am building a C++ application that uses Intel's IPP library. This library is
I'm building a large Java application that uses multiple pop-up windows. Some of these
We're building a plugin for a commercial application that uses a recent version of
I have an application that uses the .NET framework 3.5. I am building this
I am building an AJAX application that uses both HTTP Content and HTTP Header
I am building a C# application that uses GDI+ to draw images and shapes

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.