2011 7 22暑假實訓的第五天(注,這是我暑假寫的文章,來到部落格園和大家分享),跟大家分享以下我對Android location的學習吧,這是一個最基本的擷取地理位置資訊的入門,下一次給大家介紹更質能化的地理位置選擇Criteria,今天就先寫簡單的吧,挑選了SDK兩個重要的方法,我自己翻譯了一下,也加上了自己的理解,希望大家指教,開始吧!擷取跟蹤你的位置~~~
提供重要的地理位置資訊服務
1 Location Manager 管理服務
2 Location Provider 提供資料的content provider
方式一:GPS 特點:精度高,耗電量大,不耗費流量 許可權<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
方式二:NETWORK 特點:精度低,省電,需要網路訪問 許可權<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
或者許可權<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
方式三PASSIVE_PROVIDER 資料比較少,只用於特定的情景下,SDK的解釋是,並不自己執行個體化去擷取地理位置,而是通過getProvider擷取其他的服務或者activity更新位置,被動的擷取更新。
操作基本步驟:
1 在manifest.xml檔案中設定許可權
2 擷取LocaionManager
3 選擇provider
4 建立listener
/**********************************************************************************************************************/
第一部分 許可權
android的安全服務機制,如果應用要訪問本地的資源例如連絡人清單、撥號、GPS或者其他應用程式的資料,需要許可。所以要使用地理位置資訊的服務需要在<manifest>標籤下添加android:name="android.permission.ACCESS_FINE_LOCATION"/>獲得許可
也可通過單擊manifest.xml檔案的permission標籤可視化的添加許可
/**********************************************************************************************************************/
第二部分 認識LocationManager
官方SDK解釋“This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.
You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.LOCATION_SERVICE). ”
翻譯一下大概意思是“LocationManager這個類提供了對系統位置服務的訪問,這些服務允許應用程式擷取裝置地理位置的週期性更新,也可以在裝置接近一個指定的地理位置的時候發起一個指定activity的intent。你不需要建立LocationManager的執行個體,取而代之的是通過Context.getSystemService(Context.LOCATION_SERVICE)擷取。”
重要的方法:來自官方SDK(附上我的翻譯註釋)
-----------------------------------------------------------------------------------------------------------------------------------------------
public Location getLastKnownLocation (String provider)
Since: API Level 1
Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
//返回一個Location,這個location標明從給定provider獲得最後已知的位置,也就是最近擷取的位置。這個操作可以不必要啟動provider,注意這個地址可能是已經到期的,例如使用的裝置可能已經被關閉或者轉向了另一個位置。
If the provider is currently disabled, null is returned.
//如果當前的provider被禁用,函數返回null
Parameters
provider the name of the provider
Returns
the last known location for the provider, or null
Throws
SecurityException if no suitable permission is present for the provider. //前面提到的許可權的問題,沒有允許會拋出安全性的異常,android系統的機制
IllegalArgumentException if provider is null or doesn't exist //非法參數異常,表示provider為null,或者不存在
------------------------------------------------------------------------------------------------------------------------------------------------
一些補充:
認識Location類
官方SDK解釋 :“A class representing a geographic location sensed at a particular time (a "fix"). A location consists of a latitude and longitude, a UTC timestamp. and optionally information on altitude, speed, and bearing.
大致的意思是:“這個類用來表示在一個特定時間被感應的地理位置資訊(我們叫一個fix//感覺有點聚焦的意思,呵呵),一個location包括一個經度,緯度,一個世界時間戳記還有一些關于海拔,速度和方向的可選資訊。 “
可以通過getLatitude() getLongitude() getProvider()等函數來擷取封裝資訊中的經度緯度和提供資訊的provider,這個比較簡單。
-----------------------------------------------------------------------------------------------------------------------------------------------
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
Since: API Level 1
Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.
//註冊當前的activity定時的被provider的被通知。相當於註冊事件。每隔一段時間,LocationListenner會被調用,同時當前的位置或者狀態進行更新。
It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.
//接受最新的位置資訊可能會花費一會時間,如果需要立刻獲得位置資訊,程式可以使用上面講的getLastKonwnLocation方法
In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.
//假如provider被使用者停止,(比如關閉GPS等),更新會停止。並且onProviderDisabled方法(監聽器中一個需要複寫的方法)會被調用,只要provider再次變為可用狀態,onProviderEnable方法會被調用,並且更新操作立刻開始。
The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.
//通知(更新)的頻率可以通過使用minTime(最小更新時間 單位:毫秒)和minDistance(單位:米)參數,如果minTime大於0,LocationManager能夠在minTime時間內休息來儲存電量,如果minDistance大於0,每變化這個距離就會進行一次更新,如果希望儘可能頻繁的更新的資料,則把兩個參數均設定為0.
Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended.
//背景服務應該注意,設定一個合理的minTime使得裝置一直保持GPS或者WIFI的時候不耗費太多的電量。不推薦使用6000ms的minTime值
The calling thread must be a Looper thread such as the main thread of the calling Activity.
//使用該方法的線程必須時開啟訊息迴圈的線程,例如被調用activity的主線程(可以參考Android線程機制,預設建立的線程是沒有開啟訊息迴圈的,主線程開啟訊息迴圈,可以參考SDK的Looper類)
Parameters
provider the name of the provider with which to register
minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance the minimum distance interval for notifications, in meters
listener a {#link LocationListener} whose onLocationChanged(Location) method will be called for each location update//這是事件監聽器
Throws
IllegalArgumentException if provider or listener is null
RuntimeException if the calling thread has no Looper //運行時異常,在未開啟訊息迴圈的線程中運行
SecurityException if no suitable permission is present for the provider.
-----------------------------------------------------------------------------------------------------------------------------------------------
監聽器就不詳細的說了,那是java的基本知識,相信大家都很明白~
好啦,下面是我寫的一段代碼,通過GPS擷取地理位置資訊並移動監聽位置變化
http://www.cnblogs.com/octobershiner/archive/2011/10/23/2222019.html