0

I have an AsyncTask class that needs to get user's location when this AsyncTask is called.

The idea is to check if location service is enabled or not, if not, the location service toggle will popup for user to turn it on, then it will go back to the task to continue getting the location and finish the job.

However, the code below show NullPointerException on the line lat = location.getLatitude(); near the end of onPreExecute(). They said because getLastKnownLocation gets nothing as it's just been turned on a moment ago, then how can I get the location right after the location service has been turned on?

LocationManager locationManager;
LocationListener locationListener;
double lat;
double longi;

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    location = new Location(LocationManager.NETWORK_PROVIDER);
    locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(intent);
    }

    //I copy the part below from the internet but seems like "onProviderDisabled" and "onLocationChanged" were never be called
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.v("GPS CHECKKKKKKK",location.getLatitude()+"_"+location.getLongitude());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    };


    locationManager.requestLocationUpdates("gps", 500, 0, locationListener);
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,0, locationListener);
    lat = location.getLatitude();
    longi= location.getLongitude();
    Log.v("GPS Cord CHECK",lat+"_"+longi);

}

@Override
protected String doInBackground(final Checkout_Package... params) {
    //Doing the job that needs lat and longi value!
}

Thank you for your time!

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Tran Hoai Nam
  • 1,273
  • 2
  • 18
  • 35

2 Answers2

1

You cannot create and put a location listener in onPreExecute() because the onChanged handlers will only be invoked much later when onPostExecute or even your AsyncTask has finished.

What you should do instead is start an AsyncTask in that location changed handler.

So in onLocationChanged().

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

With greenapps's suggest I figured it out. I put the code into the button that call the AsyncTask

 checkout_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LocationManager locationManager;
            LocationListener locationListener;

            gotit = false;   //Boolean to start the AsyncTask only once.
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Log.v("GPS CHECKKKKKKK",location.getLatitude()+"_"+location.getLongitude());
                    lat = location.getLatitude();
                    longi = location.getLongitude();
                    if (gotit == false) {
                        new Checkout_Async(Checkout.this, listener).execute(new Checkout_Package(user, product_all, getTotalCost(product_all), fee, getTotalCost(product_all) + fee, lat, longi));
                        gotit = true;
                        pd.dismiss();
                    }
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);

                }
            };
            pd=ProgressDialog.show(Checkout.this,"",getResources().getString(R.string.please_wait),false);
            locationManager.requestLocationUpdates("gps",50,0,locationListener);

      }
   });

Now it works. Thank you!

Tran Hoai Nam
  • 1,273
  • 2
  • 18
  • 35