Android permission 動態申請、授權

來源:互聯網
上載者:User

標籤:manifest   comm   context   uil   使用   and   att   tor   資訊   

原文:Android permission 動態申請、授權

Android permission 新特性深度學習

本篇文章介紹android permission系統,並介紹android 6.0 permission的新特性,包括許可權動態申請和授權等。

permission system

Android開發人員都知道,我們開發的應用預設是沒有任何許可權的,我們沒有辦法連網,沒有辦法進行外部空間儲存(內部空間是可以的),除非我們申請了相應的許可權(permission)。

比如,我們在Manifest檔案中,加入以下語句,我們就可以在我們的應用中串連到網路。

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

那麼,android為什麼要這麼設計呢?筆者認為,這樣的設計的最大好處是,由於手機上儲存了使用者的大量隱秘資訊,對於Android的使用者來說,每次安裝App時,都會提示使用者該App擁有什麼許可權,這樣使用者對該App就有了大概的認識,該App可能會進行什麼操作,會不會竊取我的隱私等(當然,普通的Android使用者是沒有這個意識的)。

Android將所有的許可權分為了兩種,一種是Normal,另一種是Dangerous. 關於兩者,Developer是這麼介紹的,

Normal permissions cover areas where your app needs to access data or resources outside the app’s sandbox, but where there’s very little risk to the user’s privacy or the operation of other apps. For example, permission to set the time zone is a normal permission. If an app declares that it needs a normal permission, the system automatically grants the permission to the app.

總的來說,normal類型的許可權會泄露使用者的風險會很少,所以Android會預設賦予這些許可權。

Dangerous permissions cover areas where the app wants data or resources that involve the user’s private information, or could potentially affect the user’s stored data or the operation of other apps. For example, the ability to read the user’s contacts is a dangerous permission. If an app declares that it needs a dangerous permission, the user has to explicitly grant the permission to the app.

而Dangerous許可權是怎樣的呢?該類型的許可權泄露使用者隱私的風險會很大,例如,讀取使用者的通訊錄。
關於Dangerous類的許可權,Developer給出的有,

許可權的賦予

上邊介紹了危險許可權是要使用者的允許才能使用的,

before 6.0

在Android 6.0之前,這些許可權的賦予是在使用者安裝App的時候,使用者可以選擇賦予該許可權,然後安裝App,但是如果使用者不賦予的話,那麼該App就無法安裝,這就是所謂的All or Nothing。當然,這是原生系統的特性,據我所知,小米、魅族定製的系統已經可以動態賦予許可權。

after 6.0

Android 6.0對該特性進行了修改,畢竟All or Nothing的機制存在較大的弊端,很多使用者為了使用該App,就同意了所有許可權,並且無法對其進行控制,這樣就顯得許可權的作用並沒有顯現出來。所以Android 6.0 允許了動態賦予許可權。

Developer對其的說明如下,

If the device is running Android 6.0 (API level 23) or higher, and the app’s targetSdkVersion is 23 or higher, the app requests permissions from the user at run-time. The user can revoke the permissions at any time, so the app needs to check whether it has the permissions every time it runs

如果你的裝置的Android版本在6.0之上,並且App的目標sdk版本在23之上,那麼使用者就可以對其許可權進行動態賦予。而其他情況下,使用者無法動態賦予許可權,還是All or Nothing。要不全部賦予,要不就不安裝。

那麼如何使用該新特性呢?

Check For Permissions

Develper的解釋如下,

If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. The user is always free to revoke the permission, so even if the app used the camera yesterday, it can’t assume it still has that permission today.

如果我們的應用程式套件括危險許可權,那麼我們在每次執行對應的操作的時候,就應該檢查是否被授予該許可權。因為使用者可以隨時取消App的某個許可權,所以我們應該每次都要檢查使用者是否賦予了該許可權。否則,就會崩潰。

該函數的用法如下,

// Assume thisActivity is the current activityint permissionCheck = ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.WRITE_CALENDAR);

如果使用者賦予了該許可權,那麼該方法會返回PackageManager.PERMISSION_GRANTED,然後我們就可以繼續我們的操作;否則會返回PERMISSION_DENIED。如果返回PERMISSION_DENIED,我們就無法直接進行我們的操作,因為我們沒有許可權。那麼應該怎麼辦呢?此時,我們應該顯式的申請許可權。

那麼怎麼顯示的申請許可權呢?這就用到下面的函數,

requestPermissions

當我們發現,我們的應用沒有被授予某個許可權的時候,我們就可以顯示的申請。
一個例子如下,

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);

其中MY_PERMISSIONS_REQUEST_READ_CONTACTS是設定的requestCode,用來區分是申請哪個許可權,該函數會彈出一個對話方塊,詢問使用者是否授權該許可權,

使用者可以選擇授權或取消,那麼我們如何知道使用者的選擇呢,這就要求我們重載Acitivity的一個介面,

    @Override    public void onRequestPermissionsResult(int requestCode,                                           String permissions[], int[] grantResults) {        switch (requestCode) {            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {                // If request is cancelled, the result arrays are empty.                if (grantResults.length > 0                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                    // permission was granted, yay! Do the                    // contacts-related task you need to do.                    System.out.println("User granted permission");                } else {                    System.out.println("User didn‘t grante permission");                    // permission denied, boo! Disable the                    // functionality that depends on this permission.                }                return;            }            // other ‘case‘ lines to check for other            // permissions this app might request        }    }

我們根據requestCode來區分申請的許可權。當使用者選擇後,就會回調該介面,我們就可以知道使用者的選擇,進而執行下一步操作。

文中的例子,可以通過以下方式獲得,
https://github.com/KingPaul/DynamicPermissionDemo

Android permission 動態申請、授權

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.