简体   繁体   中英

Android GPS location always null inside AsyncTask

I am trying to get the current location of the user from an asynctask. My application depends on the latitude and longitude values. I am trying to show a ProgressDialog to the user till the location is fetched.

Problem :- The location value is always null. I know that getting gps location takes time. But my location value is null even after waiting for sometimes. Its always null.

Below is my code :-

   public class MainActivity extends ActionBarActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                         super.onCreate(savedInstanceState);
                             setContentView(R.layout.activity_main);

                     //some action …

            }

            @Override
                public boolean onOptionsItemSelected(MenuItem item) 

                {

                     if (id == R.id.action_settings)
                     {
                        return true;
                     }

                     if(id == R.id.action_location)
                     {
                          LocationTask locGetter = new LocationTask(MainActivity.this);
                          locGetter.execute();
                     }
                     return super.onOptionsItemSelected(item);
        }
}

Below is my AsyncTask

        public class LocationTask extends AsyncTask<Void,Void,Void> implements LocationListener
        {
                private ProgressDialog dialog;
                private Activity callingActivity;
                LocationManager locationManager;

             String provider = LocationManager.GPS_PROVIDER;

              public LocationTask(Activity activity)
                 {
                        callingActivity = activity;
                 }

                @Override
                protected void onPreExecute() 
                {
                    dialog= ProgressDialog.show(callingActivity,"Getting Co-ordinates","Please Wait....");
               }    

                @Override
                protected Void doInBackground(Void... voids) 
               {
                      locationManager = (LocationManager) callingActivity.getSystemService(Context.LOCATION_SERVICE);
                      Location location = locationManager.getLastKnownLocation(provider);
                      showLocation(location);
                          return null;
               }

                private void showLocation(Location location) 
               {

                    if(location == null)
                        {
                            Log.d("Location","Failed to get location");
                        }

                        else
                        {
                            Log.d("Location","Latitude :- "+location.getLatitude()+" Longitude :- "+location.getLongitude());
                        }
                }

                @Override
                protected void onPostExecute(Void aVoid)
                {
                        dialog.dismiss();
                        super.onPostExecute(aVoid);

                }

            @Override
            public void onLocationChanged(Location location) 
               {
                    showLocation(location);
               }    

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
}

UPDATE :-

As mentioned by Ivan I have modified my AsyncTask to get location as below :-

@Override
protected Void doInBackground(Void... voids) {
        locationManager = (LocationManager) callingActivity.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(provider,0,0,this);
        if(locationManager != null) {
            Location location = locationManager.getLastKnownLocation(provider);
            showLocation(location);
        }
return null;
}

But this throws "windows leaked" exception in the dialog= ProgressDialog.show(callingActivity,"Getting Co-ordinates","Please Wait...."); inside onPrexecute() method.

Seems to me that you might be missing the requestLocationUpdates(...) call.

Please check this related question for a better understanding on what might be missing, as it sure doesn't look to be a problem with it being inside an AsyncTask, although I don't really see the need for the AsyncTask in your snippet.

Have you tried using String locationProvider = LocationManager.NETWORK_PROVIDER; to determine if it's the provider that's the issue? Ivan has mentioned that you won't get updates, but as I understand you're still just looking for the last known location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM