简体   繁体   中英

Progress Dialog location listener

Within my programme i have a location listner used with the GPS to gain the user current lat/long points.

I want to implement a progress Dialog whilst the GPS gains the co-ordinates.

Currently I call the progressDialog within the onCreate() method then when my location object is nolonger null, then i dismess the progressdialog.

Sadly at the moment the dialog does not show at all.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new GPSLocationListener();

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

 ***** Call a new progress dialog object when the locationManager is gaining lat/long*****
 d = ProgressDialog.show(this, "GPS Posistion", "Gaining GPS posistion...", false,   true);


}

  private class GPSLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {

                ***** Once lat/long is found, dismiss the progress dialog*****
                d.dismiss();

                Double latToPass = location.getLatitude();
                Double longToPass = location.getLongitude();

                locationManager.removeUpdates(locationListener);
                locationManager = null;

                Intent changesStart = new Intent("com.example.flybaseapp.PassLatLong");
                changesStart.putExtra("passedLat", latToPass);
                changesStart.putExtra("passedLong", longToPass);
                startActivity(changesStart);

            }
        }

Use AsyncTask

    Double latToPass;
    Double longToPass;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

new AsyncAction().execute(null, null, null);

}





    private class AsyncAction extends AsyncTask<String, Void, String> {
            public boolean status = false;
            private ProgressDialog pd;

            @Override
            protected String doInBackground(String... arg0) {
                // TODO Auto-generated method stub
                try {
                    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

            locationListener = new GPSLocationListener();

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


                    status = true;

                } catch (Exception e) {
                    // TODO: handle exception
                }

                return null;
            }

            @Override
            protected void onPostExecute(String result) {

                pd.dismiss();

            Intent changesStart = new Intent("com.example.flybaseapp.PassLatLong");
                    changesStart.putExtra("passedLat", latToPass);
                    changesStart.putExtra("passedLong", longToPass);
                    startActivity(changesStart);


                }
            }

            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                pd = new ProgressDialog(MainActivity.this);
                pd.setMessage("loading...");
                pd.setIndeterminate(true);
                pd.setCancelable(false);
                pd.show();
            }

        }


      private class GPSLocationListener implements LocationListener 
        {
            @Override
            public void onLocationChanged(Location location) {
                if (location != null) {



                    latToPass = location.getLatitude();
                    longToPass = location.getLongitude();

                    locationManager.removeUpdates(locationListener);
                    //locationManager = null;


                }
            }

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