Android使用criteria選擇合適的地理位置服務(wù)實現(xiàn)方法
本文實例講述了Android使用criteria選擇合適的地理位置服務(wù)實現(xiàn)方法。分享給大家供大家參考,具體如下:
/* LocationActivity.java * @author octobershiner * 2011 7 24 * SE.HIT * 利用Criteria選擇最優(yōu)的位置服務(wù),演示定位用戶的位置并且監(jiān)聽位置變化的代碼 * */ package uni.location; import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Vibrator; import android.util.Log; import android.widget.TextView; public class LocationActivity extends Activity { /** Called when the activity is first created. */ //創(chuàng)建lcoationManager對象 private LocationManager manager; private static final String TAG = "LOCATION DEMO"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取系統(tǒng)的服務(wù), manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //創(chuàng)建一個criteria對象 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); //設(shè)置不需要獲取海拔方向數(shù)據(jù) criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); //設(shè)置允許產(chǎn)生資費 criteria.setCostAllowed(true); //要求低耗電 criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = manager.getBestProvider(criteria, false); Log.i(TAG, "we choose "+ provider); Location location = manager.getLastKnownLocation(provider); //第一次獲得設(shè)備的位置 updateLocation(location); //重要函數(shù),監(jiān)聽數(shù)據(jù)測試 manager.requestLocationUpdates(provider, 6000, 10, locationListener); } //創(chuàng)建一個事件監(jiān)聽器 private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateLocation(location); } public void onProviderDisabled(String provider){ updateLocation(null); Log.i(TAG, "Provider now is disabled.."); } public void onProviderEnabled(String provider){ Log.i(TAG, "Provider now is enabled.."); } public void onStatusChanged(String provider, int status,Bundle extras){ } }; //獲取用戶位置的函數(shù),利用Log顯示 private void updateLocation(Location location) { String latLng; if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLng = "Latitude:" + lat + " Longitude:" + lng; } else { latLng = "Can't access your location"; } Log.i(TAG, "The location has changed.."); Log.i(TAG, "Your Location:" +latLng); } }
同時修改manifest.xml文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uni.location" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LocationActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>
演示結(jié)果:
可任看到 我們只要求低的精確度并且最低電量,從最后一行可以看到我的虛擬機網(wǎng)絡(luò)服務(wù)并沒有打開,但是選擇最佳provider的時候,參數(shù)選擇了false 所以同樣可以選擇。
希望本文所述對大家Android程序設(shè)計有所幫助。
- android通過Location API顯示地址信息的實現(xiàn)方法
- 用Android Location獲取當(dāng)前地理位置的方法
- Android編程獲取地理位置的經(jīng)度和緯度實例
- Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法
- Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android百度地圖定位后獲取周邊位置的實現(xiàn)代碼
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- Android系統(tǒng)模擬位置的使用方法
- Android百度定位導(dǎo)航之基于百度地圖移動獲取位置和自動定位
- 淺析Android手機衛(wèi)士之手機實現(xiàn)短信指令獲取位置
- Android開發(fā)之Location用法實例分析
相關(guān)文章
Android中EditText 設(shè)置 imeOptions 無效問題的解決方法
有時候我們需要在EditText 輸出完之后 需要在鍵盤出現(xiàn) 右下角變成“Go”或“前往 搜索時;通常我們需要設(shè)置Android:imeOptions屬性,但是今天我發(fā)現(xiàn)設(shè)置了無效,下面給大家分享下解決方案2016-12-12淺談Android中適配器的notifyDataSetChanged()為何有時不刷新
這篇文章主要介紹了淺談Android中適配器的notifyDataSetChanged()為何有時不刷新,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Android Notification實現(xiàn)動態(tài)顯示通話時間
這篇文章主要為大家詳細介紹了Android Notification實現(xiàn)動態(tài)顯示通話時間,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09下載、編譯、運行android 7.1系統(tǒng)詳解(ubuntu 16.0.4)
Android 7的系統(tǒng)版本新增的很多的新功能,本篇文章主要介紹了基于ubuntu 16.0.4環(huán)境的下載、編譯、運行android 7.1系統(tǒng),有興趣的可以了解一下。2017-01-01unity5.6 導(dǎo)出gradle工程 Android Studio 導(dǎo)入問題及處理方法
這篇文章主要介紹了unity5.6 導(dǎo)出gradle工程 Android Studio 導(dǎo)入問題及處理方法,需要的朋友可以參考下2017-12-12Android實現(xiàn)類似網(wǎng)易新聞選項卡動態(tài)滑動效果
這篇文章主要介紹了Android實現(xiàn)類似網(wǎng)易新聞選項卡動態(tài)滑動效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11android同時控制EditText輸入字符個數(shù)和禁止特殊字符輸入的方法
這篇文章主要介紹了android同時控制EditText輸入字符個數(shù)和禁止特殊字符輸入的方法,涉及Android操作EditText控制字符操作的技巧,需要的朋友可以參考下2015-04-04