1. 程式人生 > >android------關於API 23的許可權問題

android------關於API 23的許可權問題

在程式碼:

locationManager.requestLocationUpdates(provider, 60000, 50, getListener());
中報以下的錯誤:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`

在AndroidManifest.xml中加入了以下的許可權還是有錯:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
百度了下,大意是:

呼叫需要的許可權可能被使用者拒絕:程式碼應該顯式地檢查許可權是否可用(用checkPermission)或顯式地處理一個潛在的“SecurityException”

這是在使用 Android marshmallow(API 23)時出現的

參考以下程式碼解決:

public class MainActivity extends AppCompatActivity {

    /* GPS Constant Permission */
    private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11;
    private static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 12;

    /* Position */
    private static final int MINIMUM_TIME = 10000;  // 10s
    private static final int MINIMUM_DISTANCE = 50; // 50m

    /* GPS */
    private String mProviderName;
    private LocationManager mLocationManager;
    private LocationListener mLocationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Get the best provider between gps, network and passive
        Criteria criteria = new Criteria();
        mProviderName = mLocationManager.getBestProvider(criteria, true);

        // API 23: we have to check if ACCESS_FINE_LOCATION and/or ACCESS_COARSE_LOCATION permission are granted
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            // No one provider activated: prompt GPS
            if (mProviderName == null || mProviderName.equals("")) {
                startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }

            // At least one provider activated. Get the coordinates
            switch (mProviderName) {
                case "passive":
                    mLocationManager.requestLocationUpdates(mProviderName, MINIMUM_TIME, MINIMUM_DISTANCE, this);
                    Location location = mLocationManager.getLastKnownLocation(mProviderName);
                    break;

                case "network":
                    break;

                case "gps":
                    break;

            }

        // One or both permissions are denied.
        } else {

            // The ACCESS_COARSE_LOCATION is denied, then I request it and manage the result in
            // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        MY_PERMISSION_ACCESS_COARSE_LOCATION);
            }
            // The ACCESS_FINE_LOCATION is denied, then I request it and manage the result in
            // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
                ActivityCompat.requestPermissions(this,
                        new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                        MY_PERMISSION_ACCESS_FINE_LOCATION);
            }

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSION_ACCESS_COARSE_LOCATION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted
                } else {
                    // permission denied
                }
                break;

            case MY_PERMISSION_ACCESS_FINE_LOCATION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted
                } else {
                    // permission denied
                }
                break;
            }

        }
    }
}


相關推薦

android中window和windowManager原始碼分析(android-api-23

一、前言 在android中window無處不在,如activity、dialog、toast等。它是view所依附的載體,每個window都對應於一個View和一個ViewRootImpl。ViewRootImpl就是Window和view的連線紐帶。windowMana

android學習筆記—— ”vnd.android-dir/mms-sms “ 在 android api 23 後不再被支援

   如題。常有自家app 調起 簡訊的需求,大部分寫法都會如下,但其中這句 <span style="white-space:pre"> </span>smsIntent.

android------關於API 23許可權問題

在程式碼: locationManager.requestLocationUpdates(provider, 60000, 50, getListener()); 中報以下的錯誤: Call requires permission which may be rejecte

AndroidAPI 23動態獲取許可權完美解決

首先感謝CSDN給予我們這個共同交流,共同學習的機會。 其次感謝其他大神技術的共享。 下面呢就開始我們的學習之旅啦,開車了。 Android 6.0 相比之前的Android版本有一個很大的不同點,就是動態獲取許可權。今天自己在做拍照功能時,正好遇到這個問

執行時許可權的處理方法(以打電話為例 Api 23,24)

Android中危險許可權共9組24個許可權 group:android.permission-group.CONTACTS permission:android.permission.WRITE_CONTACTS permission:android

android studio api 23 android 6.0 requires android.permission.READ_CONTACTS or android.permiss

Causedby: java.lang.SecurityException:PermissionDenial: opening provider com.android.providers.contacts.ContactsProvider2fromProcessRecor

Android Api Demos登頂之路(四十五)Loader--&gt;Cursor

adapt lists 清空 function icon 創建 nal pat lines 這個demo演示了類載入器的用法。關於類載入器的使用我們在前面的demo中已經介紹過了 在此再小小的復習一下。類載入器的使用步驟: * 1.獲取類載入

Android API Guides---Host-based Card Emulation

source 過程 特定 裏的 term 安裝 visa 保護 action Host-based Card Emulation 很多提供NFC功能的Andr??oid手機已經支持NFC卡模擬。在大多數情況下。該卡是由在該裝置的單獨芯片仿真,稱為安全元件。無線運營

Android API Guides---Searchable Configuration

default 最小 conf lena attr mda ati 請求 -c Searchable Configuration 為了實現與Android系統(提供搜索查詢到活動中,並提供搜索建議)協助搜索,應用程序必須提供一個XML文件的形式搜索配置。

Android API level 與version對應關系

rsh padding border jelly ice android ble 4.2 hone Platform Version API Level VERSION_CODE 中文名稱 Android API 26 26 Android 7.1.1

Android API 28 使用 android-async-http1.4.8 報錯

專案升級 API 28後,使用 android-async-http1.4.8 時,報錯如下: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory;

Android API 28 配置 networkSecurityConfig

在一個專案中既用到了 科大訊飛聽寫API,又要訪問自己的伺服器(非HTTPS)。 而當升級到Android API 28時,需要配置networkSecurityConfig 才能訪問伺服器, 如https://blog.csdn.net/xyx2999/article/details/82

Android API 28 訪問伺服器失敗 提示CLEARTEXT

解決方法: 參考https://blog.csdn.net/castanea/article/details/81256422 在res下新建一個xml目錄 建立名為network_security_config.xml 檔案 ,該檔案內容如下: <?xml version="1.

Apache Http Client 在Android SDK 23中被移除

Android SDK API level 升級到23以後(Android 6.0)去掉了對於Apache Http Client的支援,Google的建議是用HttpURLConnection(API level 9以上),Google解釋說這個API的效率更高,它可以通過壓縮和響應快取減少

Android api level對照表

經常要查api對應的level,特此暫記,待有更新,再增補 Platform Version API Level VERSION_CODE Notes Android 8.1

android 6.0許可權管理

轉載 https://blog.csdn.net/android2me/article/details/69525975 private void checkPermission() { //檢查許可權(NEED_PERMISSION)是否被授權 PackageManager.

Android API Level與sdk版本對照表

API等級1: Android 1.0 API等級2: Android 1.1 Petit Four 花式小蛋糕 API等級3: Android 1.5 Cupcake 紙杯蛋糕 API等級4: Android 1.6 Donut 甜甜圈 API等級5: Android 2.0 Éc

Android 動態申請 許可權 permission

文章目錄 1、單個許可權動態申請 1)檔案結構 2) 在 AndroidManifest.xml 新增許可權 3)在程式碼裡進行許可權檢查 和 申請 2

【舉例】Android動態申請許可權

寫在前面:對於新版本的Android,部分許可權必須手動向使用者發起請求並獲得允許後才能使用。關於這一部分的基礎知識,這裡就不詳述。大家可以自行百度去獲取。 相關知識: 【舉例】Android自定義Dialog——做出“確定/取消”的選擇 http://blog.cs

android 模擬器 sdcard許可權修改

android 模擬器 sdcard許可權修改 2015年01月14日 13:41:07 逆流向上的魚 閱讀數:722 標籤: android修改模擬器許可權 android sdk 更多 個人分類: android and