Q:

GPS code returning last location

All I wanted to do is to get the current location using GPS before I move to next activity.  So I disabled the buttons that takes me to next activities.  The buttons are enabled only after getting a location using the GPS.  The following code runs perfectly.  But I found one bug.. 
Last time I used the app it was quite right.  Then I moved to my University.  There I launched the app and then moved to next activity as location was retrieved by then.  I got  the location of my home which is about 10 kilometers away.  Then after closing the application, I launched it once again and this time it retrieved the correct location.  
Well, this is happening because of the line below:
        location = locationManagerAsync.getLastKnownLocation(providerAsync);

My app is retrieving the last location and enabling the buttons.  But it is able to keep the current location to use for the next time I launch the app.  As a result, on the second attempt, the location is retrieved perfectly.  
I used a code from a question on stackoverflow.com which is given below.  
private class MyLocationTracker extends AsyncTask<Void, Void, Void> implements LocationListener {

    private Context ContextAsync;
    public MyLocationTracker (Context context){
        this. ContextAsync = context;
    }

    Dialog progress;
    private String providerAsync;
    private LocationManager locationManagerAsync;  
    double latAsync=0.0;
    double lonAsync=0.0;
    Location location;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progress = ProgressDialog.show(ContextAsync, "Loading location", "Please wait...");
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        locationManagerAsync = (LocationManager) ContextAsync.getSystemService(ContextAsync. LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria. ACCURACY_COARSE);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria. NO_REQUIREMENT);
        providerAsync = locationManagerAsync.getBestProvider(criteria, false);

        if (locationManagerAsync.isProviderEnabled(LocationManager. GPS_PROVIDER)) {
            providerAsync = LocationManager. GPS_PROVIDER;
        } else if (locationManagerAsync.isProviderEnabled(LocationManager. NETWORK_PROVIDER)) {
            providerAsync = LocationManager. NETWORK_PROVIDER;

        } else if (locationManagerAsync.isProviderEnabled(LocationManager. PASSIVE_PROVIDER)) {
            providerAsync = LocationManager. PASSIVE_PROVIDER;
            //Toast.makeText(ContextAsync, "Switch On Data Connection!!!!",  Toast. LENGTH_LONG).show();
        }    

        location = locationManagerAsync.getLastKnownLocation(providerAsync);

        // Initialize the location fields
        if (location ! = null) {
            //  System.out.println("Provider " + provider + " has been selected."); 
            latAsync = location.getLatitude();
            lonAsync = location.getLongitude();

        } else {
            //Toast.makeText(ContextAsync, " Location not available", Toast. LENGTH_SHORT).show();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {  

        super.onPostExecute(result);
        progress.dismiss();
        onLocationChanged(location);
        Log.v("latAsync_lonAsync",latAsync+"_"+lonAsync);

        myCurrentLatitude = latAsync;
        myCurrentLongitude = lonAsync;

        // the buttons that were disabled in the onCreate() method  
        police.setEnabled(true);
        medical.setEnabled(true);
         fireService.setEnabled(true);
        cityCorporation.setEnabled(true);
        telePhnCall.setEnabled(true);
        getRoute.setEnabled(true);          

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

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

    }

}

And this is the line that I am using to call the AsyncTask from the Oncreate() method..
    new MyLocationTracker(TypeOfEmergency.this).execute();

Now the problem is, I do not want the last location to be retrieved and stored in the variables.  I tried the code after removing the line 
    location = locationManagerAsync.getLastKnownLocation(providerAsync);

But the app crashes in that case.  Need a quick fix.  
Here is the error log :: (Please have a look to the comments before having a look to the error log)

A:

With the code something is basically wrong: the single line of code in onLocationChanged
locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this);

needs to be called at the end of doInBackground (which does not need to be done in background).  This line of code requests location updates from the provider (e.g. the GPS provider).  When a new location is avaiable the onLocationChanged of your location listener is called (which is MyLocationTracker in your example due to the this in the line above.  in onLocationChanged you need to react to a new location by enabling or disabling the functionality, you mentioned in your question. 
The call to getLastKnownLocation does exectly what the name suggests: It returns the last known position, which may be quite old.