首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LocationListener InSide AsyncTask

LocationListener InSide AsyncTask
EN

Stack Overflow用户
提问于 2011-07-23 03:22:28
回答 2查看 2.8K关注 0票数 2

嗨,我是android编程新手,目前正在开发一个使用位置管理器来获取用户位置并在地图上放置标记的应用程序。我正在尝试使用AsyncTask来运行LocationListener,并在用户位置发生变化时不断更新标记。这就是我正在学习的课程...

代码语言:javascript
复制
public class IncidentActivity extends MapActivity{


   public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.incidentactivity);

       mapView = (MapView)findViewById(R.id.mapView);
       mapView.setBuiltInZoomControls(true);   
       mapView.setTraffic(true);


        mapController = mapView.getController();
        String coordinates[] = {"-26.167004","27.965505"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
        geoPoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
        mapController.animateTo(geoPoint);
        mapController.setZoom(16);
        mapView.invalidate();

         new MyLocationAsyncTask().execute();

   }



      private class MyLocationAsyncTask extends AsyncTask<Void, Location, Void> implements LocationListener{
        private  double latLocation;
        private Location l;

        //location management variables to track and maintain user location
        protected LocationManager locationManager;
        protected LocationListener locationListener;

        @Override
        protected Void doInBackground(Void... arg0) {
            Looper.prepare();
              locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener);

              this.publishProgress(l);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
        }

        @Override
        protected void onProgressUpdate(Location... values) {

            super.onProgressUpdate(values);
        }

        //this method is never executed i dont know why...?
        public void onLocationChanged(Location location) {
            if (location != null){
                latLocation = location.getLatitude(); 
                Toast.makeText(getBaseContext(), " Your latLocation :" + latLocation, Toast.LENGTH_LONG).show();
                //Log.d("Your Location", ""+latLocation);
            }
        }

        public void onProviderDisabled(String provider) {


        }

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

        }

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

        }


      }


}
EN

回答 2

Stack Overflow用户

发布于 2012-11-26 02:56:48

我刚刚实现了这样的AsyncTask

代码语言:javascript
复制
class GetPositionTask extends AsyncTask<Void, Void, Location> implements LocationListener
{
    final long TWO_MINUTES = 2*60*1000;
    private Location location;
    private LocationManager lm;

    protected void onPreExecute()
    {
        // Configure location manager - I'm using just the network provider in this example
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
        nearProgress.setVisibility(View.VISIBLE);
    }

    protected Location doInBackground(Void... params)
    {
        // Try to use the last known position
        Location lastLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        // If it's too old, get a new one by location manager
        if (System.currentTimeMillis() - lastLocation.getTime() > TWO_MINUTES)
        {
            while (location == null)
                try { Thread.sleep(100); } catch (Exception ex) {}

            return location;
        }

        return lastLocation;
    }

    protected void onPostExecute(Location location)
    {
        nearProgress.setVisibility(View.GONE);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.removeUpdates(this);

        // HERE USE THE LOCATION
    }

    @Override
    public void onLocationChanged(Location newLocation)
    {
        location = newLocation;
    }

    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}
票数 2
EN

Stack Overflow用户

发布于 2011-07-25 01:19:30

根据我所阅读和尝试的内容,您不能在ASyncTask中使用循环程序(locationlistener需要它)。Click Here

实际上这意味着两个线程模型不兼容,所以你不能同时使用它们。循环希望拥有与其关联的线程,而AsyncTask拥有它为您创建的线程,以便在后台运行。因此,它们彼此冲突,不能一起使用。

戴安·哈克伯恩建议使用HandlerThread,但我成功地让我的产品在IntentService中工作。我承认我的代码还是有点问题。

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

https://stackoverflow.com/questions/6794998

复制
相关文章

相似问题

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