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.
- android-Location Strategies
- Android API Guides---Location Strategies
- 【翻譯】android developer api指南翻譯 location strategies
- strategies
- Android Location
- Android Location
- Concurrency Strategies
- 定制 android system location
- Android map and location
- Android Location and Maps
- Android Location的使用
- android install location
- android install location
- Android GPS Location null
- 【android】Location-based Service
- Android Using Location Manager
- android mock location
- android mock location
- cellForRowAtIndexPath不执行
- 我们和他们,究竟谁是傻X? | 华尔街黑历史(一)
- SOUI开篇之作
- ECharts Java 动态加载数据,echartsjava
- iOS 页面组织 显示规则
- android-Location Strategies
- 使用mailx发送邮件
- State Threads 回调终结者
- TAO和ACE在Windows平台下的编译与安装
- 递归求平均数
- appium的几点总结
- Linux下配置hadoop2.2.0集群
- java中反射的原理
- Linux软件包管理器之rpm和yum详解