Take the 2-minute tour ×

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";

public void onCreate(Bundle savedInstanceState) {

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



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

// 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)

}// 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.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);

    {   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) {
        this.activity = activity;
        this.longitude = longitude;
        this.latitude = latitude;

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

    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
            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;

    protected void onPostExecute(Result result) {
        Toast.makeText(activity.getApplicationContext(), "Finished.", 

//Location Listener
public class MyLocationListener implements LocationListener

    public void onLocationChanged(Location location) {
        double lng = location.getLatitude();
        double lat = location.getLongitude();
        MyAsyncTask task = new MyAsyncTask(ConfirmScreen.this, lng, lat);
        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();


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

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


        // TODO Auto-generated method stub



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.

share|improve this question
Send your Email in onLocationChanged! –  Herrmann Oct 2 '12 at 14:05

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
Thanks! That's helped! I think I have a working solution with a small issue. –  Chris O'Brien Oct 2 '12 at 22:42

Your Answer


