0

I use the code provided by Fedor in the following link, in order to get the latitude and longitude from my simple demo app. I am trying to fetch the latitude and longitude using the MyLocation class provided by him in that link.

What is the simplest and most robust way to get the user's current location on Android?

I try to fetch the latitude and longitude on a button click. On the button click, I start an async task and delegate the location fetching work to the do in background method of my asynctask.

pre execute - progressdialog initiated. post execute - progress dialog dismissed.

This is how, the progress dialog in my code should work and here is the issue which I have. THe progress dialog gets initiated correctly, but even before the latitude and longitude gets printed in the doinbackground method, the progress dialog gets dismissed.

I do not understand why this happens.

Here is my front end activity

public class LocationServices extends Activity {
MyLocation myLocation = new MyLocation();
LocationResult locationResult;
TextView tv1, tv2;
Location location;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);


    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            new LocationAsyncTasking().execute();

        }
    });

}

public class LocationAsyncTasking extends AsyncTask<String, Void, Void> {

    ProgressDialog dialog;
    int totalAvail;

    protected void onPreExecute() {

        // this.dialog.setMessage("Inserting data...");
        dialog = new ProgressDialog(LocationServices.this);
        this.dialog.setMessage("Fetching data...");
        this.dialog.show();
    }

    protected Void doInBackground(String... args) {
        Looper.prepare();

        locationResult = new LocationResult() {

            public void gotLocation(Location location) {
                // TODO Auto-generated method stub

                // LocationServices.this.location = location;
                System.out.println("Progress dialog should be present now - latitude"+location.getLatitude());
                System.out.println("Progress dialog should be present now - longitude"+location.getLongitude());
            }
        };
        myLocation.getLocation(LocationServices.this, locationResult);

        return (null);
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(Void unused) {
        dialog.dismiss();
    }

}

}

I am quite puzzled, thinking of what makes this progress dialog disappear even before the SOP in doinbackground is finished.

Experts, please help me understand and resolve this issue.

Any help in this regard is well appreciated.

Looking forward, Best Regards, Rony

Community
  • 1
  • 1
user264953
  • 1,837
  • 8
  • 37
  • 63

2 Answers2

1

I found all of the examples rather confusing being new to Android programming, in the end ... the simplest was the one that worked, so this answer is based on the comment above that has the code pasted into it, but I've attached my full activity which works .. in that the labels are updated with the location after you pressed the button. Thought it make someone's life easier. Oh, and make sure you use the MyLocation class as mentioned above.

package com.benderapp;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.benderapp.MyLocation.LocationResult;

public class DefaultActivity extends Activity {
    MyLocation myLocation = new MyLocation();
    TextView tv1, tv2;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView)findViewById(R.id.tv2);
    btn = (Button)findViewById(R.id.Button01);

    final MyLocation myLocation = new MyLocation();
        final LocationResult locationResult = new LocationResult() {

            @Override
            public void gotLocation(Location location) {
                    // TODO Auto-generated method stub
                    tv1.setText(location.getLatitude() + "");//need to add latitude i get here
                    tv2.setText(location.getLongitude() + "");//need to add longitude i get here                       
            }
    };


    btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    myLocation.getLocation(DefaultActivity.this,locationResult);
            }
        });     
    }
}
Steven Elliott
  • 3,214
  • 5
  • 29
  • 40
0

Something like that. Just add ProgressDialog if you need it.

public class LocationServices extends Activity {
    MyLocation myLocation = new MyLocation();
    TextView tv1, tv2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);

        Button btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myLocation.getLocation(LocationServices.this, locationResult);
            }
        });

    }

    public LocationResult locationResult = new LocationResult() {
            public void gotLocation(Location location) {
                runOnUiThread(new Runnable(){
                    @Override
                    public void run(){
                        tv1.setText(location.getLatitude());
                        tv2.setText(location.getLongitude());
                    }
                });
            }
        };
}
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • Sorry, I cannot see the method to add code here, so i am pasting it here directly.. – user264953 Dec 28 '10 at 12:29
  • final MyLocation myLocation = new MyLocation(); final LocationResult locationResult = new LocationResult() { @Override public void gotLocation(Location location) { // TODO Auto-generated method stub } }; btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myLocation.getLocation(LocationServices.this,locationResult); tv1.setText("latitude");//need to add latitude i get here tv2.setText("longitude");//need to add longitude i get here } }); – user264953 Dec 28 '10 at 12:38
  • I did as per your suggestion, but how can I get loc.getlat() and loc.getlon() in order to set the values to the textview I have...also, before setting these values, I do not want the user to perform any other action inside the activity..so in order to block him from doing that and to display the other components in background while the lat, long is fetched, I should use a spinner, rt? – user264953 Dec 28 '10 at 12:38
  • 12-28 23:24:24.276: ERROR/AndroidRuntime(524): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. ---------- I get this exception, when I try to update the two textviews in the -- public void gotLocation(Location location) method ...detailed code here for reference -- http://pastebin.com/FThEkBzC – user264953 Dec 28 '10 at 18:17