获取AsyncTask中的GPS位置

时间:2016-08-18 18:25:48

标签: android android-asynctask gps

我有一个AsyncTask课程,需要在调用此AsyncTask时获取用户的位置。

想法是检查是否启用了位置服务,如果没有,将弹出位置服务切换以供用户打开,然后它将返回任务以继续获取位置并完成作业。

但是,下面的代码会在NullPointerException末尾附近的lat = location.getLatitude();行显示onPreExecute()。他们之所以说getLastKnownLocation因为它刚刚被启用而没有得到任何结果,那么我怎样才能在定位服务开启后立即获得该位置?

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!
}

感谢您的时间!

2 个答案:

答案 0 :(得分:1)

您无法在onPreExecute()中创建和放置位置侦听器,因为onChanged处理程序仅在onPostExecute甚至AsyncTask完成后才会被调用。

你应该做的是在该位置更改处理程序中启动AsyncTask。

所以在onLocationChanged()

答案 1 :(得分:0)

有了greenapps的建议,我想出来了。我将代码放入调用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);

      }
   });

现在它有效。谢谢!