2013-06-01 36 views
0

我试图让这段代码排序几次。下面是到目前为止,我已经尝试了情景......使用AsyncTask获取GPS坐标的问题

场景: 一旦活动开始时我想要得到的坐标(长& LAT)基于网络/ GPS提供商。这应该在后台运行,它应该继续检查,直到long long & lat不为空或“0.0”。因此,我已经使用AsyncTask尝试了以下代码,从而在doInBackground方法中使用了LocationListener。

源代码:

public class GetLocation extends AsyncTask<String, String, String> 
{ 
    private myTest test; 
    boolean running =true; 
    private Context cont; 
    String addressString; 

      public GetLocation(myTest fr, Context contxt) 
      { 
       test = fr; 
       cont = contxt; 
      } 

      @Override 
      protected void onPreExecute() 
      { 
       super.onPreExecute();     

      } 

      @Override 
      protected void onProgressUpdate(String... values) { 
       super.onProgressUpdate(values); 

      } 

      @Override 
      protected void onPostExecute(String result) 
      { 
       test.GetContent(); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       Looper.myLooper().prepare(); 

       LocationManager locationManager; 
       locationManager = (LocationManager) cont 
         .getSystemService(Context.LOCATION_SERVICE); 

       Criteria crta = new Criteria(); 
       crta.setAccuracy(Criteria.ACCURACY_FINE); 
       crta.setAltitudeRequired(false); 
       crta.setBearingRequired(false); 
       crta.setCostAllowed(true); 
       crta.setPowerRequirement(Criteria.POWER_LOW); 
       String provider = locationManager.getBestProvider(crta, true); 

       Location location = locationManager.getLastKnownLocation(provider); 
       updateWithNewLocation(location); 
       LocationListener locationListener = new LocationListener() { 

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

        @Override 
        public void onProviderDisabled(String provider) { 
         updateWithNewLocation(null); 
        } 

        @Override 
        public void onProviderEnabled(String provider) { 
        } 

        @Override 
        public void onStatusChanged(String provider, int status, 
          Bundle extras) { 
        } 

       }; 

       locationManager.requestLocationUpdates(provider, 0, 0, 
         locationListener); 
       Looper.loop(); 

       return addressString; 
      } 

      private void updateWithNewLocation(Location location) { 

       Constants.lat = Double.toString(location.getLatitude()); 
      Constants.long = Double.toString(location.getLongitude());   

      } 
} 

问题: 不过,我也知道,活套的可以帮助保持线程活跃。我希望能够获得有效的坐标,并且这应该循环直到收到。我如何打电话来一次又一次地得到地点,直到收到有效的地址为止? (我可以在我提供的循环中添加条件,但是我不知道如何以及如何调用这些方法来实现此目的)。请尽可能提供代码片段。

干杯!

+0

请检查下面的答案。 http://stackoverflow.com/questions/7709030/get-gps-location-in-a-broadcast-receiver-or-service-to-broadcast-receiver-data-t/7709140#7709140 – itsrajesh4uguys

+0

我试过这个代码。我设法使服务运行,但我没有看到ReceiverPositioningAlarm在任何情况下,当我调试。 –

回答

0

你已经实现了这个错误。没有必要创建AsyncTaskLocationManager将为您异步获取位置并在您的LocationListener.onLocationChanged上传送它们。一旦你得到一个合适的Location,你可以拨打LocationManager.removeUpdates(),这将停止进一步交付Locations

+0

是的,但我想等到收到正确的位置。那么我应该从onLocationChanged方法放回一个回调函数吗? –

0

不要为此使用AsyncTask。您只需注册由LocationListener侦听的LocationManager并使用它的进度条。一旦调用LocationListner.onLocationChanged尝试获取Lat和Long并关闭进度条。 OnLocationChanged将在您短距离移动设备时进行调用。

+0

所以我应该关闭onlocationchanged方法中的进度条吗?因为我不知何故需要继续检查是否收到适当的坐标。 –