【问题标题】:adding AsyncTask for searching location添加 AsyncTask 用于搜索位置
【发布时间】:2012-11-13 03:06:32
【问题描述】:

所以我正在申请获得当前职位。下面的代码工作正常

字符串字符串地址 = ""; 公共无效getLocation(查看视图){ 最终 LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 标准 kriteria = new Criteria(); kriteria.setAccuracy(Criteria.ACCURACY_FINE); kriteria.setAltitudeRequired(false); kriteria.setBearingRequired(false); kriteria.setCostAllowed(true); kriteria.setPowerRequirement(Criteria.POWER_LOW); 最终字符串提供者 = lm.getBestProvider(kriteria, true); 最终位置 lokasi = lm.getLastKnownLocation(provider); updateWithNewLocation(lokasi); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, ll); edittext_position.setText(stringAddress); } 私人无效updateWithNewLocation(位置 如果(lokasi!= null){ 双纬度 = lokasi.getLatitude(); 双 lng = lokasi.getLongitude(); Geocoder gc = new Geocoder(this, Locale.getDefault()); 尝试{ 列出地址 = gc.getFromLocation(lat, lng, 1); StringBuilder sb = new StringBuilder(); 如果(地址。大小()> 0){ 地址地址=addresses.get(0); sb.append(address.getAddressLine(0)); stringAddress= sb.toString(); } } 捕捉(异常 e){ } } } 私人最终 LocationListener ll = new LocationListener() { public void onStatusChanged(String provider, int status, Bundle extras) { } 公共无效onProviderEnabled(字符串提供者){ } 公共无效onProviderDisabled(字符串提供者){ updateWithNewLocation(null); } 公共无效onLocationChanged(位置位置){ updateWithNewLocation(位置); } }

但问题是每次我调用 getLocation() 函数时,应用程序都会在返回结果之前挂起几秒钟。我知道使用 aSyncTask 解决这个问题,但我不知道如何开始。感谢您的帮助。

谢谢

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    这是我的应用程序中的 sn-p:

    public void getCurrentLocation(final ListenerGetCurrentLocation listenerGetCurrentLocation) {
        new AsyncTask<Void, Void, List<Address>>() {
    
            @Override
            protected List<Address> doInBackground(Void... voids) {
                Geocoder geo = new Geocoder(instance);
    
                List<Address> listAddresses = null;
    
                Criteria criteria = new Criteria();
                String bestProvider = locationManager.getBestProvider(criteria, true);
    
                if (bestProvider == null) {
                    bestProvider = LocationManager.NETWORK_PROVIDER;
                }
    
                Location location = locationManager.getLastKnownLocation(bestProvider);
    
                try {
                    if (location != null) {
                        listAddresses = geo.getFromLocation(location.getLatitude(), 
                                                            location.getLongitude(), 
                                                            1);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                return listAddresses;
            }
    
            public void onPostExecute(List<Address> listAddresses) {                
                Address _address = null;
                if ((listAddresses != null) && (listAddresses.size() > 0)) {
                    _address = listAddresses.get(0);
    
                    GeoPoint currentPosition = new GeoPoint(((int)(_address.getLatitude() * 1E6)), 
                                                            ((int)(_address.getLongitude() * 1E6)));
                }
    
            }
    
        }.execute();
    }
    

    【讨论】:

    • 嗨,很抱歉我的回复迟了。我现在试试你的解决方案。谢谢
    【解决方案2】:

    requestLocationUpdates() 是异步的,它不会阻止您的应用。它在后台轮询位置,然后调用监听器。

    但是,gc.getFromLocation() 不是。这可能是你滞后的原因

    创建一个新的AsyncTask,Eclipse 会建议你重写那些方法

    @Override
    protected List<String> doInBackground(String... params) {
       // this is done in the background so it won't block the UI. The return type must be set to List<String> (I think default is just String)
       return gc.getFromLocation()
    }
    
    
    @Override
    protected void onPostExecute(List<String> adresses) {
        // called when the GC work is finished. You can now use your list of addresses and display them
    }
    

    不要忘记在您的任务上致电execute()

    【讨论】:

    • 对不起,我迟到了。我会试试你的解决方案
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2010-12-29
    • 2012-01-12
    • 1970-01-01
    相关资源
    最近更新 更多