2014-11-04 74 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