android-Location Strategies

来源:互联网 发布:linux nc 编辑:程序博客网 时间:2024/05/30 23:35

It also handles location update scheduling based on power consumption parameters you provide.

 Android's Network Location Provider determines user location using cell tower and Wi-Fi signals, providing location information in a way that works indoors and outdoors, responds faster, and uses less battery power. To obtain the user location in your application, you can use both GPS and the Network Location Provider, or just one.GPS, Cell-ID, and Wi-Fi can each provide a clue to users location. 

For example, the following code shows how to define a LocationListener and request location update:

// Acquire a reference to the system Location ManagerLocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);// Define a listener that responds to location updatesLocationListener locationListener = new LocationListener() {    public void onLocationChanged(Location location) {      // Called when a new location is found by the network location provider.      makeUseOfNewLocation(location);    }    public void onStatusChanged(String provider, int status, Bundle extras) {}    public void onProviderEnabled(String provider) {}    public void onProviderDisabled(String provider) {}  };// Register the listener with the Location Manager to receive location updateslocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
To request location updates from the GPS provider, substitute GPS_PROVIDER for NETWORK_PROVIDER

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

As demonstrated above, you can begin listening for updates by calling requestLocationUpdates():

String locationProvider = LocationManager.NETWORK_PROVIDER;// Or, use GPS location data:// String locationProvider = LocationManager.GPS_PROVIDER;locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

 Until a more accurate location is provided to your location listener, you should utilize a cached location by callinggetLastKnownLocation(String):

String locationProvider = LocationManager.NETWORK_PROVIDER;// Or use LocationManager.GPS_PROVIDERLocation lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

Always beware that listening for a long time consumes a lot of battery power, so as soon as you have the information you need, you should stop listening for updates by callingremoveUpdates(PendingIntent):

// Remove the listener you previously addedlocationManager.removeUpdates(locationListener);
Adjusting the model to save battery and data exchange

As you test your application, you might find that your model for providing good location and good performance needs some adjustment. Here are some things you might change to find a good balance between the two

Reduce the size of the window

A smaller window in which you listen for location updates means less interaction with GPS and network location services, thus, preserving battery life. But it also allows for fewer locations from which to choose a best estimate.

Set the location providers to return updates less frequently

Reducing the rate at which new updates appear during the window can also improve battery efficiency, but at the cost of accuracy. The value of the trade-off depends on how your application is used. You can reduce the rate of updates by increasing the parameters in requestLocationUpdates() that specify the interval time and minimum distance change.

Restrict a set of providers

Depending on the environment where your application is used or the desired level of accuracy, you might choose to use only the Network Location Provider or only GPS, instead of both. Interacting with only one of the services reduces battery usage at a potential cost of accuracy.

>There are three different ways to send your application mock location data: using Eclipse, DDMS, or the "geo" command in the emulator console.

Note: Providing mock location data is injected as GPS location data, so you must request location updates from GPS_PROVIDER in order for mock location data to work.

0 0