成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法

1.添加權(quán)限--6.0之后要?jiǎng)討B(tài)獲取,下面會(huì)說

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、龍海網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、龍海網(wǎng)絡(luò)營(yíng)銷、龍海企業(yè)策劃、龍海品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供龍海建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:jinyejixie.com

<uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION"/>

2.直接上代碼,不多說,代碼中注釋很詳細(xì)。

private static final int BAIDU_READ_PHONE_STATE = 100;//定位權(quán)限請(qǐng)求
private static final int PRIVATE_CODE = 1315;//開啟GPS權(quán)限
/**
 * 檢測(cè)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)版本,如果是,需要?jiǎng)討B(tài)添加權(quán)限
   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
     != PERMISSION_GRANTED) {// 沒有權(quán)限,申請(qǐng)權(quán)限。
    ActivityCompat.requestPermissions(this, LOCATIONGPS,
      BAIDU_READ_PHONE_STATE);
   } else {
    getLocation();//getLocation為定位方法
   }
  } else {
   getLocation();//getLocation為定位方法
  }
 } else {
  Toast.makeText(this, "系統(tǒng)檢測(cè)到未開啟GPS定位服務(wù),請(qǐng)開啟", 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)自動(dòng)生成的,不加會(huì)出錯(cuò)**/
 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申請(qǐng)權(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 這個(gè)方法主要是動(dòng)態(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)限,沒有開啟會(huì)一直讓用戶開啟權(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.動(dòng)態(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ǔ)知識(shí)寫的不好,大佬勿噴,謝謝!

以上這篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。

文章名稱:Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
URL分享:http://jinyejixie.com/article16/iejjdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
洛隆县| 姜堰市| 高台县| 南华县| 漳浦县| 朔州市| 固阳县| 凤山市| 台前县| 东丽区| 雷山县| 连云港市| 迁西县| 札达县| 闵行区| 澄江县| 黄浦区| 葵青区| 松溪县| 平昌县| 湾仔区| 扶余县| 进贤县| 冕宁县| 贵州省| 泸西县| 惠水县| 岳普湖县| 剑川县| 黄陵县| 香港| 榆树市| 嘉兴市| 任丘市| 景东| 耿马| 尖扎县| 临朐县| 阿巴嘎旗| 施甸县| 佳木斯市|