2014-11-04 115 views
0

我試圖編寫一個應用程序,它使用用戶當前的位置並計算距離並將其寫入我的列表視圖。android在後臺獲取位置並更新列表視圖

位置不一定非常準確,我只想在刷新列表或在應用程序啓動時獲取新位置,而不是連續。

我的問題是,與GPS全球定位儀需要太長時間才能找到一個位置,我必須更新我的清單很多之前,它顯示正確的距離。

我在考慮實現一個後臺任務,它獲取位置並在找到位置時自動更新列表。這是一個解決方案嗎?

是否有任何選項可以更快地獲得位置,即使它不如gps準確?

我至今在我的位置監聽器:

public class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      lat = location.getLatitude(); 
      lng = location.getLongitude(); 

      myLoc.setLatitude(lat); 
      myLoc.setLongitude(lng); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    } 
在這種方法

我打電話的LocationManager和監聽器和距離創建列表視圖

public void getList(){ 
    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener); 

//... creating the list with distance and so on 
} 

我希望你能給我一些提示,我可以如何實現這一點,我將如上所述工作,或告訴我應該使用什麼。 謝謝:)

+0

您可以使用getLastKnownLocation方法更快地獲取位置。您也可以嘗試從網絡獲取位置。嘗試閱讀位置API的更多細節 – 2014-11-04 14:04:37

回答

1

1)。您可以使用LocationManager.NETWORK_PROVIDER

此提供程序根據小區塔和WiFi接入點的可用性來確定位置。通過網絡查找檢索結果。需要android.permission.ACCESS_COARSE_LOCATION或android.permission.ACCESS_FINE_LOCATION的權限。

例如: - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,500,50,locationListener);

2)。如果要使用後臺任務,請使用此服務

public class LocationFinder extends Service { 
    public static double lat, lng; 

    LocationManager locationManager; 

    public void onDestroy() { 

     super.onDestroy(); 

     if (locationManager != null && locationListener != null) { 
      locationManager.removeUpdates(locationListener); 
     } 

    } 

    @Override 
    public void onCreate() { 
     Log.v("location", "===>location ed onCreate " + lat); 

     super.onCreate(); 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, flags, startId); 

     Log.v("location", "===>location ed onStartCommand " + lat + "==>" + lng); 
     new Handler().post(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       getLocation(); 

      } 
     }); 
     return START_STICKY; 
    } 

    final LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      updateWithNewLocation(location); 
     } 

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

     public void onProviderEnabled(String provider) { 
     } 

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

    private void getLocation() { 
     String context = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager) getSystemService(context); 

     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 

     if (locationManager != null) { 
      String provider = locationManager.getBestProvider(criteria, true); 
      if (provider != null) { 
       Location location = locationManager.getLastKnownLocation(provider); 
       updateWithNewLocation(location); 
       locationManager.requestLocationUpdates(provider, 500, 50, locationListener); 
      } else { 
       if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener); 

       } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener); 
       } else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) { 
        locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener); 
       } 
      } 

     } 
    } 

    private void updateWithNewLocation(Location location) { 
     if (location != null) { 
      Log.v("location", "===>location ed " + lat); 

      lat = location.getLatitude(); 
      lng = location.getLongitude(); 

     } 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 
+0

非常感謝:)。你認爲有必要把它作爲我的目的的背景任務嗎? – user3780814 2014-11-04 14:26:56