Android使用criteria選擇合適的地理位置服務實現方法
本文實例講述了Android使用criteria選擇合適的地理位置服務實現方法。分享給大家供大家參考,具體如下:
/* LocationActivity.java * @author octobershiner * 2011 7 24 * SE.HIT * 利用Criteria選擇最優(yōu)的位置服務,演示定位用戶的位置并且監(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)的服務, manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //創(chuàng)建一個criteria對象 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); //設置不需要獲取海拔方向數據 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); //設置允許產生資費 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); //第一次獲得設備的位置 updateLocation(location); //重要函數,監(jiān)聽數據測試 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){ } }; //獲取用戶位置的函數,利用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>
演示結果:
可任看到 我們只要求低的精確度并且最低電量,從最后一行可以看到我的虛擬機網絡服務并沒有打開,但是選擇最佳provider的時候,參數選擇了false 所以同樣可以選擇。
希望本文所述對大家Android程序設計有所幫助。
- android通過Location API顯示地址信息的實現方法
- 用Android Location獲取當前地理位置的方法
- Android編程獲取地理位置的經度和緯度實例
- Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法
- Android打開GPS導航并獲取位置信息返回null解決方案
- android通過gps獲取定位的位置數據和gps經緯度
- Android百度地圖定位后獲取周邊位置的實現代碼
- Android獲取當前位置的經緯度數據
- Android系統(tǒng)模擬位置的使用方法
- Android百度定位導航之基于百度地圖移動獲取位置和自動定位
- 淺析Android手機衛(wèi)士之手機實現短信指令獲取位置
- Android開發(fā)之Location用法實例分析
相關文章
Android中EditText 設置 imeOptions 無效問題的解決方法
有時候我們需要在EditText 輸出完之后 需要在鍵盤出現 右下角變成“Go”或“前往 搜索時;通常我們需要設置Android:imeOptions屬性,但是今天我發(fā)現設置了無效,下面給大家分享下解決方案2016-12-12淺談Android中適配器的notifyDataSetChanged()為何有時不刷新
這篇文章主要介紹了淺談Android中適配器的notifyDataSetChanged()為何有時不刷新,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07Android Notification實現動態(tài)顯示通話時間
這篇文章主要為大家詳細介紹了Android Notification實現動態(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 導出gradle工程 Android Studio 導入問題及處理方法
這篇文章主要介紹了unity5.6 導出gradle工程 Android Studio 導入問題及處理方法,需要的朋友可以參考下2017-12-12android同時控制EditText輸入字符個數和禁止特殊字符輸入的方法
這篇文章主要介紹了android同時控制EditText輸入字符個數和禁止特殊字符輸入的方法,涉及Android操作EditText控制字符操作的技巧,需要的朋友可以參考下2015-04-04