Android使用位置管理器
只需要進(jìn)行一些簡單的設(shè)置,你的應(yīng)用程序就可以接受位置更新,在這次教程里你將詳細(xì)的學(xué)習(xí)這些步驟,讓你掌握Android位置管理器的使用方法。
一、在Manifest里聲明合適的權(quán)限
要想獲取位置更新,***步需要在manifest里聲明合適的權(quán)限。如果忘了聲明相應(yīng)的權(quán)限,那么你的應(yīng)用在運行時會報安全異常。當(dāng)你使用LocationManagement方法的時候,需要設(shè)置權(quán)限ACCESS_CORASE_LOCATION或者 ACCESS_FINE_LOCATION,例如,如果你的應(yīng)用使用了基于網(wǎng)絡(luò)的信息服務(wù),你需要聲明N ACCESS_CORASE_LOATION權(quán)限,要想獲取GPS請求你需要聲明ACCESS_FINE_LOCATION權(quán)限。值得注意的是如果你聲明了ACCESS_FINE_LOCATION權(quán)限隱含著你也聲明了ACCESS_CORASE_LOCATION權(quán)限。 假如一個應(yīng)用使用了基于網(wǎng)絡(luò)的位置的信息服務(wù),你需要聲明因特網(wǎng)權(quán)限。
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
二、獲得一個位置管理的引用
LocationManager是一個主類,在android里你通過這個類你可以使位置服務(wù)。使用方法類似于其他的服務(wù),通過調(diào)用 getSystemService方法可以獲得相應(yīng)的引用。如果你的應(yīng)用想要在前臺(在Activity里)獲得位置更新,你應(yīng)該在onCreate() 里執(zhí)行以下語句。
- LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
三、挑選一個位置提供者
當(dāng)沒有請求的時候,現(xiàn)在大部分android電源管理可以通過多種底層技術(shù)可以獲得位置更新,這種技術(shù)被抽象為LocationProvider類的應(yīng) 用。在時間、精度、成本、電源消耗等方面,位置提供者有不同的運行特性。通常,像GPS,一個精確的位置提供者,需要更長的修正時間,而不是不精確,比如 基于網(wǎng)絡(luò)的位置提供者。 通過權(quán)衡之后你必須選擇一種特殊的位置提供者,或者多重提供者,這些都依賴與你的應(yīng)用的客戶需求。例如,比如說一個關(guān)鍵點的簽到服務(wù),需要高精度定位,而 一個零售商店定位器使用城市級別的修正就可以滿足。下面的代碼段要求一個GPS提供者的支持。
- LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
你提供一些輸入標(biāo)準(zhǔn),比如精度、功率需求、成本等等,讓android決定一個最合適的位置匹配提供者。下邊的代碼片段需要的是更精確的位置提供者而不是 考慮成本。需要注意的是這個標(biāo)準(zhǔn)不能幫你解決任何的提供者,可能返回值為空。這個時候你的應(yīng)用應(yīng)該能夠很好的處理這種情況
- // Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- criteria.setCostAllowed(false);
- ...
- String providerName = locManager.getBestProvider(criteria, true);
- // If no suitable provider is found, null is returned.
- if (providerName != null) {
- ...
- }
四、檢查位置提供者是否使能
在設(shè)置里,一些位置提供者比如GPS可以被關(guān)閉。良好的做法就是通過調(diào)用isProviderEnabled()方法來檢測你想要的位置提供者是否打開。如果位置提供者被關(guān)閉了,你可以在設(shè)置里通過啟動Intent來讓用戶打開。
- @Override
- protected void onStart() {
- super.onStart();
- // This verification should be done during onStart() because the system calls
- // this method when the user returns to the activity, which ensures the desired
- // location provider is enabled each time the activity resumes from the stopped state.
- LocationManager locationManager =
- (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- if (!gpsEnabled) {
- // Build an alert dialog here that requests that the user enable
- // the location services, then when the user clicks the "OK" button,
- // call enableLocationSettings()
- }
- }
- private void enableLocationSettings() {
- Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivity(settingsIntent);
- }
希望本文讓讀者朋友們對學(xué)習(xí)Android位置管理器一定的幫助和啟發(fā)。