如何在Android的AsyncTask后台线程中获取当前的GPS位置?

时间:2018-07-05 06:22:38

标签: android android-asynctask location android-gps background-thread

实际上,我想根据需要在AsyncTask后台线程中获取当前不在活动中的GPS位置,并希望将GPS位置返回到android中的活动。 请指导我该怎么做?

这是通过使GPS当前位置在活动中处于完美状态而实现的代码,但是我想在AsyncTask中执行相同的操作:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detaildiscount);
  locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                SetGpsLocation(location);
                Log.d(TAG, "onLocationChanged: ");
                // textView.append("n " + location.getLongitude() + " " + location.getLatitude());
            }

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

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }
        };

        configure_button();
}

请参阅以下功能:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case 10:
            configure_button();
            Log.d(TAG, "onRequestPermissionsResult: ");
            break;
        default:
            break;
    }
}

void configure_button() {
    // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
                    , 10);
            Log.d(TAG, "configure_button: ");
        }
        Log.d(TAG, "configure_button: ");
        return;
    }
    RequestGpsLocation();
    // this code won'textView execute IF permissions are not allowed, because in the line above there is return statement
}

public void RequestGpsLocation() {
    //noinspection MissingPermission
    if (ActivityCompat.checkSelfPermission(DetailOfListCard.this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(DetailOfListCard.this, Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "RequestGpsLocation: ");
        return;
    }
    Log.d(TAG, "RequestGpsLocation: ");
    locationManager.requestLocationUpdates("gps", 5000, 0, listener);
}

预先感谢

1 个答案:

答案 0 :(得分:0)

在该位置已更改的处理程序和onLocationChanged()中启动AsyncTask。 将以下代码放入调用AsyncTask的乐趣中

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

        LocationManager locationManager;
        LocationListener locationListener;

        startAsync = false;   //to start the AsyncTask once.
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.v("GPS",location.getLatitude()+"_"+location.getLongitude());
                lat = location.getLatitude();
                longi = location.getLongitude();
                if (startAsync == false) {
                    new Checkout_Async(Checkout.this, listener).execute(new Checkout_Package(user, product_all, getTotalCost(product_all), fee, getTotalCost(product_all) + fee, lat, longi));
                    startAsync = 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);

  }
   });