Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
1.添加權(quán)限--6.0之后要動態(tài)獲取,下面會說
<uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION"/>
2.直接上代碼,不多說,代碼中注釋很詳細(xì)。
private static final int BAIDU_READ_PHONE_STATE = 100;//定位權(quán)限請求 private static final int PRIVATE_CODE = 1315;//開啟GPS權(quán)限
/**
* 檢測GPS、位置權(quán)限是否開啟
*/
public void showGPSContacts() {
lm = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (ok) {//開了定位服務(wù)
if (Build.VERSION.SDK_INT >= 23) { //判斷是否為android6.0系統(tǒng)版本,如果是,需要動態(tài)添加權(quán)限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PERMISSION_GRANTED) {// 沒有權(quán)限,申請權(quán)限。
ActivityCompat.requestPermissions(this, LOCATIONGPS,
BAIDU_READ_PHONE_STATE);
} else {
getLocation();//getLocation為定位方法
}
} else {
getLocation();//getLocation為定位方法
}
} else {
Toast.makeText(this, "系統(tǒng)檢測到未開啟GPS定位服務(wù),請開啟", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, PRIVATE_CODE);
}
}
/**
* 獲取具體位置的經(jīng)緯度
*/
private void getLocation() {
// 獲取位置管理服務(wù)
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服務(wù)信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 獲取GPS信息
/**這段代碼不需要深究,是locationManager.getLastKnownLocation(provider)自動生成的,不加會出錯**/
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(provider); // 通過GPS獲取位置
updateLocation(location);
}
/**
* 獲取到當(dāng)前位置的經(jīng)緯度
* @param location
*/
private void updateLocation(Location location) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LogUtil.e("維度:" + latitude + "\n經(jīng)度" + longitude);
} else {
LogUtil.e("無法獲取到位置信息");
}
}
/**
* Android6.0申請權(quán)限的回調(diào)方法
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
// requestCode即所聲明的權(quán)限獲取碼,在checkSelfPermission時(shí)傳入
case BAIDU_READ_PHONE_STATE:
//如果用戶取消,permissions可能為null.
if (grantResults[0] == PERMISSION_GRANTED && grantResults.length > 0) { //有權(quán)限
// 獲取到權(quán)限,作相應(yīng)處理
getLocation();
} else {
showGPSContacts();
}
break;
default:
break;
}
}
onRequestPermissionsResult 這個方法主要是動態(tài)獲取6.0權(quán)限,返回時(shí)的回調(diào),我這里需求是獲取權(quán)限之后獲取到當(dāng)前位置的經(jīng)緯度詳細(xì)信息
3.下面是當(dāng)點(diǎn)擊獲取GPS定位,跳轉(zhuǎn)到系統(tǒng)開關(guān),ActivityResult回調(diào),我這里做的是必須要開啟GPS權(quán)限,沒有開啟會一直讓用戶開啟權(quán)限,怎么決定,看具體需求
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PRIVATE_CODE:
showContacts();
break;
}
}
4.動態(tài)權(quán)限設(shè)置添加多條權(quán)限
static final String[] LOCATIONGPS = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_PHONE_STATE};
注:代碼很詳細(xì)!基礎(chǔ)知識寫的不好,大佬勿噴,謝謝!
以上這篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android編程下拉菜單spinner用法小結(jié)(附2則示例)
這篇文章主要介紹了Android編程下拉菜單spinner用法,結(jié)合實(shí)例較為詳細(xì)的總結(jié)分析了下拉菜單Spinner的具體實(shí)現(xiàn)步驟與相關(guān)技巧,并附帶兩個示例分析其具體用法,需要的朋友可以參考下2015-12-12
Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動查看圖文詳情
這篇文章主要為大家詳細(xì)介紹了Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動查看圖文詳情,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android自定義Animation實(shí)現(xiàn)View搖擺效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Animation實(shí)現(xiàn)View搖擺效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
android studio 的下拉菜單Spinner使用詳解
這篇文章主要介紹了android studio 的下拉菜單Spinner使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android實(shí)現(xiàn)環(huán)形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)環(huán)形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android?WebView的使用與后退鍵處理詳細(xì)討論
在android開發(fā)中我們有時(shí)候根據(jù)項(xiàng)目的需求多少會加載一些webview,加載webview,我們有時(shí)候會根據(jù)UI來自定義返回鍵,下面這篇文章主要給大家介紹了關(guān)于Android?WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下2024-04-04
Android System fastboot 介紹和使用教程
Fastboot是Android快速升級的一種方法,Fastboot的協(xié)議fastboot_protocol.txt在源碼目錄./bootable/bootloader/legacy下可以找到,本文給大家介紹Android System fastboot 介紹和使用教程,感興趣的朋友一起看看吧2024-01-01
Android編程實(shí)現(xiàn)TextView部分顏色變動的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView部分顏色變動的方法,涉及Android針對TextView樣式操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
淺析Android手機(jī)衛(wèi)士之抖動輸入框和手機(jī)震動
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之輸入框抖動和手機(jī)震動的相關(guān)資料,需要的朋友可以參考下2016-04-04

