首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AsyncTask的doInBackground块locationManager

AsyncTask的doInBackground块locationManager
EN

Stack Overflow用户
提问于 2016-03-16 12:34:08
回答 1查看 274关注 0票数 1

我只想找到用户的位置一次,并显示一个ProgressDialog (PD),直到这个位置是未知的。

我使用的是AsyncTask,在onPreExecute()中显示PD,调用locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);。因为使用GPS查找位置可能需要很长时间,所以在doInBackground()中,我通过调用listener.isLocationAvailable()来检查位置是否可用。

在用for(int i = 0; i < 100; i++) {...}循环替换while循环之后,输出将是下一个:

代码语言:javascript
运行
复制
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling

100次..。

代码语言:javascript
运行
复制
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333

(在调用onLocationChanged(位置位置)时,在100次循环后显示100倍)

下面是AsyncTask:

代码语言:javascript
运行
复制
 private class PositioningTask extends AsyncTask<Void, Void, String> {
    Context context = TransportActivity.this;
    ProgressDialog progressDialog;

    TransportLocationListener listener;
    LocationManager locationManager;
    private boolean gpsEnabled;
    private boolean networkEnabled;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(TransportActivity.this);
        progressDialog.setTitle("Loading position, please wait");
        progressDialog.setMessage(getString(R.string.please_wait));
        progressDialog.setCancelable(false);
        progressDialog.show();

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new TransportLocationListener();
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        }
    }

    @Override
    protected String doInBackground(Void... params) {
        String position = "";
        try{
            while (true){
                if (listener.isLocationAvailable()) {
                    Location location = listener.getLocation();
                    position = location.getLatitude() + "," + location.getLongitude();

                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                            || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        locationManager.removeUpdates(listener);
                    }
                    System.out.println("POSITION: " + position);
                    break;
                }
                System.out.println("cycling");
                Thread.sleep(1000);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        return position;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();
    }
}

TransportLocationListener:

代码语言:javascript
运行
复制
public class TransportLocationListener implements LocationListener {
private Location location = null;
private Boolean available = false;

public boolean isLocationAvailable() {
    return  available;
}

public Location getLocation() {
    return location;
}

@Override
public void onLocationChanged(Location location) {
    this.location = location;

    available = true;

    System.out.println(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) {

}

}

EN

回答 1

Stack Overflow用户

发布于 2016-03-16 13:20:11

AsyncTask的doInBackground块locationManager

我想您的问题是,您得到的位置更新源源不断?

以下是您的请求:

代码语言:javascript
运行
复制
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

是以minTime = 0和minDistance = 0接收位置更新。这意味着,即使位置没有改变,'onLocationChanged‘也会被调用。将此值更改为1000和0.1f,以减少使用实际新值调用onLocationChanged的频率。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36035833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档