Android聲明權(quán)限

如何向AndroidManifest中添加權(quán)限?

將<uses-permission>元素作為頂級(jí)<manifest>元素的子項(xiàng)。例如發(fā)送短信的權(quán)限可在AndroidManifest添加以下代碼行:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.snazzyapp">

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

        <application...>
            ...
        </application>

    </manifest>

正常權(quán)限和危險(xiǎn)權(quán)限

  • 正常權(quán)限涵蓋應(yīng)用訪問(wèn)其沙盒外部數(shù)據(jù)或資源,但對(duì)用戶隱私或其他應(yīng)用操作風(fēng)險(xiǎn)很小的區(qū)域,如獲取時(shí)區(qū)的權(quán)限。
  • 危險(xiǎn)權(quán)限涵蓋應(yīng)用涉及用戶隱私信息的數(shù)據(jù)或資源,或者可能對(duì)用戶存儲(chǔ)的數(shù)據(jù)或其他應(yīng)用的操作產(chǎn)生影響的區(qū)域。如讀取用戶聯(lián)系人屬于危險(xiǎn)權(quán)限。

系統(tǒng)版本和應(yīng)用targetSdkVersion對(duì)聲明權(quán)限的影響

  • 設(shè)備運(yùn)行在Android 5.1或更低版本,或targetSdkVersion為22或更低
    如果在清單中列出了危險(xiǎn)權(quán)限,用戶在安裝應(yīng)用時(shí)授予此權(quán)限,如果用戶不授予此權(quán)限,系統(tǒng)不會(huì)安裝應(yīng)用。
  • 設(shè)備運(yùn)行在Android 6.0或更高版本,或targetSdkVersion為23或更高
    應(yīng)用必須在清單中列出權(quán)限,且必須在運(yùn)行時(shí)請(qǐng)求其需要的每項(xiàng)危險(xiǎn)權(quán)限。用戶可授予或拒絕每項(xiàng)權(quán)限,且即使用戶拒接權(quán)限請(qǐng)求,應(yīng)用仍可繼續(xù)運(yùn)行有限的功能。

檢查權(quán)限

要檢查是否具有某項(xiàng)權(quán)限,可調(diào)用ContextCompat.checkSelfPermission()方法。例如:

  /* 
    *如果應(yīng)用具有此權(quán)限,方法返回 PackageManager.PERMISSION_GRANTED
    *如果應(yīng)用不具有此權(quán)限,方法返回 PackageManager.PERMISSION_DENIED
     */
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR);

請(qǐng)求您需要的權(quán)限

請(qǐng)求權(quán)限:requestPermissions()

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_CONTACTS)) {

        } else {
            // No explanation needed, we can request the permission.
            // 當(dāng)應(yīng)用調(diào)用requestPermissions()時(shí),系統(tǒng)向用戶顯示一個(gè)標(biāo)準(zhǔn)對(duì)話框
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }

響應(yīng)權(quán)限請(qǐng)求

在應(yīng)用請(qǐng)求權(quán)限時(shí),系統(tǒng)將向用戶顯示一個(gè)對(duì)話框。當(dāng)用戶響應(yīng)后,系統(tǒng)將調(diào)用onRequestPermissionsResult()方法,向其傳遞用戶作出響應(yīng)。例如:

@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.

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容