上一篇文章 【Android筆記 二】Location獲取地理位置信息(上)介紹了簡單的地理位置服務的使用,今天這篇文章給大家分享以下Criteria類的使用
注意在locationManager中還有一個重要的方法就是getBestProvider()
------------------------------------------------------------------------------------------------------------------------------------------------
public String getBestProvider (Criteria criteria, boolean enabledOnly)
Since: API Level 1
Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed by the calling activity will be returned. If several providers meet the criteria, the one with the best accuracy is returned. If no provider meets the criteria, the criteria are loosened in the following sequence:
//翻譯:返回滿足給定的criteria(標準)的最佳provider,(前面一篇文章提到過兩種重要provider的區別)。只有當provider權限允許的時候纔會返回,如果幾個provider均滿足criteria設定的標準,最貼近標準的provider將會被返回。如果沒有provider滿足,標準將按照下面的序列放寬鬆。
power requirement
accuracy
bearing
speed
altitude
Note that the requirement on monetary cost is not removed in this process.
//在放寬標準的過程中,對於資費的要求不會被移除,我感覺也就是是否允許產生資費的標準不會改變
Parameters
criteria the criteria that need to be matched
enabledOnly if true then only a provider that is currently enabled is returned//這個參數爲true的時候,只會返回當前可用的provider,例如GPS處於關閉狀態時,就不會獲得了
Returns
name of the provider that best matches the requirements
------------------------------------------------------------------------------------------------------------------------------------------------
下面詳細的看一下Criteria類的解釋
A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.
翻譯:“這個類表示了應用程序選擇位置服務provider的一個標準,provider可能是根據精準度,電量使用,能否獲得海拔,速度、方向和產生資費來選擇的”也就是說,criteria給用戶提供了多種因素的標準設置,locationManager可以根據這個設定好的標準,自動選擇最慢組需求的provider。
------------------------------------------------------------------------------------------------------------------------------------------------
void setAccuracy(int accuracy)
Indicates the desired accuracy for latitude and longitude. //設置經緯度的精準度 可選參數有ACCURACY_FINE 準確 ACCURACY_COARSE 粗略
void setAltitudeRequired(boolean altitudeRequired)
Indicates whether the provider must provide altitude information.//設置是否需要獲取海拔數據
void setBearingAccuracy(int accuracy)
Indicates the desired bearing accuracy. //設置方向的精確 可選參數有ACCURACY_LOW,低 ACCURACY_HIGH 高 NO_REQUIREMENT.沒有要求
void setBearingRequired(boolean bearingRequired)
Indicates whether the provider must provide bearing information.//設置是否需要獲得方向信息
void setCostAllowed(boolean costAllowed)
Indicates whether the provider is allowed to incur monetary cost. //設置是否允許定位過程中產生資費,比如流量等
void setHorizontalAccuracy(int accuracy)
Indicates the desired horizontal accuracy (latitude and longitude).//獲取水平方向經緯度的精準度 可選參數ACCURACY_LOW, 低 ACCURACY_MEDIUM 中 ACCURACY_HIGH 高 NO_REQUIREMENT. 無要求
void setPowerRequirement(int level)
Indicates the desired maximum power level. //設置耗電量的級別
void setSpeedAccuracy(int accuracy)
Indicates the desired speed accuracy. //設置速度的精確度
void setSpeedRequired(boolean speedRequired)
Indicates whether the provider must provide speed information. //設置是否提供速度的要求
void setVerticalAccuracy(int accuracy)
Indicates the desired vertical accuracy (altitude).//設置垂直距離的海拔高度
這裏是我寫的一段演示代碼