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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:18:37+00:00 2026-06-12T03:18:37+00:00

I’m trying to get a GPS location and parse it to a String to

  • 0

I’m trying to get a GPS location and parse it to a String to in turn parse that to a clickable URL. I’ve done the code to get the location, which works, but because it can take a while to get a location, the String was executing before the location was parsed. I’ve tried messing around with Process Dialogs etc, but I can’t get it to work.

See Code

public class ConfirmScreen extends Activity{

String mapCoord = "http://maps.google.com";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm_screen);


    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mLocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocListener);


    sendEmail();
    playSound();

}


public void backHome(View view) 
{
    Intent intent = new Intent (this, MainScreen.class);
    startActivity(intent);
}

// Method to start playing and looping a sound.

public void playSound()
{
    MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean soundCheck = sp.getBoolean("SOUND", false);
    if (soundCheck)
    {
        clickSound.start();
    }



}// method end



public void sendEmail()
{
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    String nameValue = sp.getString("NAME", "failed to get name");
    String emailValue = sp.getString("EMAIL", "failed to get email");
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{emailValue});
    i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
    i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. Please see the co-ords attached and run to their rescue!" +
            " If you don't see any co-ords, they didn't check the box and assume you know where they are.\nKind Regards\nDon't Panic! \n\n\n" +  mapCoord);

    try
    {   startActivity(Intent.createChooser(i, "Send mail...."));
    } 
    catch (android.content.ActivityNotFoundException ex){

        Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
    }
}

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();
    }
    }



//Location Listener
public class MyLocationListener implements LocationListener
{


    @Override
    public void onLocationChanged(Location location) {
        double lng = location.getLatitude();
        double lat = location.getLongitude();
        MyAsyncTask task = new MyAsyncTask(ConfirmScreen.this, lng, lat);
        task.execute();
        Toast.makeText(getApplicationContext(), "Done :D", Toast.LENGTH_LONG).show();

        String text = "Current Location is \nLat: " + lng + " \nLng: " + lat;
        mapCoord =  Double.toString(lng) + " " + Double.toString(lat);

        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
    return true;
}






}

Compiles with no errors but it doesn’t do what I intend. I’ve been read others code and trying to implement onto my own but I’m probably messing up something.

1) onCreate gets location
2) A dialog box should pop up until GPS co-ords are received
3) Those co-ords should be passed to a String
4) An email is sent with the co-ords (this works fine)
5) Sound plays if box checked (again this works)

I was getting the co-ords but the difficulty was that the email method was being called before the app has the chance to get the co-ords. I don’t want to use lastKnownCo-ords.

  • 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-12T03:18:39+00:00Added an answer on June 12, 2026 at 3:18 am

    1/ No. onCreate requests that at some point in the future the location will be given to mLocListener, but anyway after onCreate has returned. onCreate also starts a new Activity to write an email. No coordinates are retrieved at htis point.

    2/ No. A dialog box opens when GPS coordinates are received.

    3/ No exactly. The coordinates are passed to a geocoder and transformed to an address. I don’t know why since you don’t use this address (except for display)

    4/ No. The coords string is created in 3/, but the email is sent in 1/, hence much before.

    What you actually want to do is to send the email after you received the coordinates, that is, in the onLocationChanged method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
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
I'm trying to select an H1 element which is the second-child in its group
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.