Android获取asynctask的位置

时间:2014-10-10 05:46:39

标签: android android-asynctask android-location

我使用此代码获取当前位置,此代码工作正常,但现在需要放 这个代码在Asynctask类中,我不知道如何在Asynctask上实现这段代码

请帮帮我怎么做

   public class GetLoc implements LocationListener{
        SharedPreferences preferences = null;
        SharedPreferences.Editor editor = null;
        Context context;
        LocationManager locationManager ;
        String provider;
         double lati;
         double logi;
        ProgressDialog pd;
        public static String state;
        String zz;
        public GetLoc(Context context)
        {
            this.context= context;
             locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
            //Toast.makeText(context, "Location can't be retrieved", Toast.LENGTH_SHORT).show();
             Criteria criteria = new Criteria();
            //  pd = ProgressDialog.show(context, "","Please wait...                       "); 
                // Getting the name of the provider that meets the criteria
                provider = locationManager.getBestProvider(criteria, false);
                if(provider!=null && !provider.equals("")){


                    // Get the location from the given provider 
                    Location location = locationManager.getLastKnownLocation(provider);

                    locationManager.requestLocationUpdates(provider, 20000, 1, this);


                    if(location!=null)
                        onLocationChanged(location);
                    else
                        Toast.makeText(context, "Location can't be retrieved", Toast.LENGTH_SHORT);

                }else{
                    Toast.makeText(context, "No Provider Found", Toast.LENGTH_SHORT);
                }


        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location.getLatitude()>0 && location.getLongitude()>0)
            {
                lati=location.getLatitude();
                logi=location.getLongitude();
            state=  getAddress(context, lati, logi);
            }
        //  Toast.makeText(context, "No"+zz, Toast.LENGTH_SHORT).show();
        //  pd.dismiss();
        }

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

        }

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

        }

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

        }

        public  String getAddress(Context ctx, double latitude, double longitude) {
            StringBuilder result = new StringBuilder();
            try {
                Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                if (addresses.size() > 0) {
                    Address address = addresses.get(0);

                    //String locality=address.getLocality();
                    //String city=address.getCountryName();
                    //String region_code=address.getCountryCode();
                    state= address.getAdminArea();
                  //  zipcode=address.getPostalCode();
                    double lat =address.getLatitude();
                    double lon= address.getLongitude();

                 //   result.append(locality+" ");
                 //   result.append(city+" "+ region_code+" ");
                    //result.append(zipcode);

                }
            } catch (IOException e) {
              //  Log.e("tag", e.getMessage());
            }

            return state;
        }

    }

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

        @Override
        protected void onPreExecute() {
            mVeggsterLocationListener = new VeggsterLocationListener();
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0,
                    mVeggsterLocationListener);

            progDailog = new ProgressDialog(FastMainActivity.this);
            progDailog.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    FetchCordinates.this.cancel(true);
                }
            });
            progDailog.setMessage("Loading...");
            progDailog.setIndeterminate(true);
            progDailog.setCancelable(true);
            progDailog.show();

        }

        @Override
        protected void onCancelled(){
            System.out.println("Cancelled by user!");
            progDialog.dismiss();
            mLocationManager.removeUpdates(mVeggsterLocationListener);
        }

        @Override
        protected void onPostExecute(String result) {
            progDailog.dismiss();

            Toast.makeText(FastMainActivity.this,
                    "LATITUDE :" + lati + " LONGITUDE :" + longi,
                    Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            while (this.lati == 0.0) {

            }
            return null;
        }

        public class VeggsterLocationListener implements LocationListener {

            @Override
            public void onLocationChanged(Location location) {

                int lat = (int) location.getLatitude(); // * 1E6);
                int log = (int) location.getLongitude(); // * 1E6);
                int acc = (int) (location.getAccuracy());

                String info = location.getProvider();
                try {

                    // LocatorService.myLatitude=location.getLatitude();

                    // LocatorService.myLongitude=location.getLongitude();

                    lati = location.getLatitude();
                    longi = location.getLongitude();

                } catch (Exception e) {
                    // progDailog.dismiss();
                    // Toast.makeText(getApplicationContext(),"Unable to get Location"
                    // , Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onProviderDisabled(String provider) {
                Log.i("OnProviderDisabled", "OnProviderDisabled");
            }

            @Override
            public void onProviderEnabled(String provider) {
                Log.i("onProviderEnabled", "onProviderEnabled");
            }

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                Log.i("onStatusChanged", "onStatusChanged");

            }

        }

    }

}