Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(xiàn)方法
本文實(shí)例講述了Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(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對(duì)象
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)建一個(gè)criteria對(duì)象
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
//設(shè)置不需要獲取海拔方向數(shù)據(jù)
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
//設(shè)置允許產(chǎn)生資費(fèi)
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ù)測(cè)試
manager.requestLocationUpdates(provider, 6000, 10,
locationListener);
}
//創(chuàng)建一個(gè)事件監(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);
}
}
同時(shí)修改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é)果:

可任看到 我們只要求低的精確度并且最低電量,從最后一行可以看到我的虛擬機(jī)網(wǎng)絡(luò)服務(wù)并沒有打開,但是選擇最佳provider的時(shí)候,參數(shù)選擇了false 所以同樣可以選擇。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android通過Location API顯示地址信息的實(shí)現(xiàn)方法
- 用Android Location獲取當(dāng)前地理位置的方法
- Android編程獲取地理位置的經(jīng)度和緯度實(shí)例
- Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法
- Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- Android系統(tǒng)模擬位置的使用方法
- Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位
- 淺析Android手機(jī)衛(wèi)士之手機(jī)實(shí)現(xiàn)短信指令獲取位置
- Android開發(fā)之Location用法實(shí)例分析
相關(guān)文章
flutter?Bloc?add兩次只響應(yīng)一次問題解析
這篇文章主要為大家介紹了flutter?Bloc?add兩次只響應(yīng)一次問題解析記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android編程實(shí)現(xiàn)將ButtonBar放在屏幕底部的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將ButtonBar放在屏幕底部的方法,涉及Android界面設(shè)計(jì)與文本操作相關(guān)技巧,需要的朋友可以參考下2017-03-03
Android仿優(yōu)酷圓形菜單學(xué)習(xí)筆記分享
這篇文章主要為大家分享了Android仿優(yōu)酷圓形菜單學(xué)習(xí)筆記,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android中的AtomicLong原理、使用與實(shí)戰(zhàn)指南
本文詳細(xì)介紹了AtomicLong在Android多線程開發(fā)中的應(yīng)用,包括其核心原理、基本使用、適用場(chǎng)景、生產(chǎn)環(huán)境實(shí)戰(zhàn)案例以及性能優(yōu)化建議,通過大量Kotlin代碼示例,幫助開發(fā)者更好地理解和使用AtomicLong,感興趣的朋友一起看看吧2025-03-03
Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能
這篇文章主要為大家詳細(xì)介紹了Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能,感興趣的小伙伴們可以參考一下2016-05-05
Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法
這篇文章主要介紹了Android調(diào)用系統(tǒng)圖片裁剪限定尺寸,及7.0照相問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android用代碼獲取手機(jī)root之后的最高權(quán)限
機(jī)得root之后通過代碼可以獲得最高權(quán)限如果沒有root的話請(qǐng)不要往下看,毫無意義,root之后的朋友可以參考下本文或許有意想不到的收獲2013-03-03

