我正在檢查並獲得API級別爲23及以上的用戶的許可。因此,這裏是一個令人困惑的事情對我來說,android.com說:如果應用程序已請求此權限以前與用戶拒絕了這一要求shouldShowRequestPermissionRationale未按預期工作
shouldShowRequestPermissionRationale()方法返回true。 如果用戶拒絕了在過去的許可請求,並選擇在許可請求的系統對話框中不要再次詢問選項,這種方法在另一邊返回false
它給檢查的權限,並要求下面的代碼如果neccessery
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
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.
}
}
else
範圍在上面的例子中運行的權限,如果用戶不允許許可和檢查不要再次詢問,對不對?因此,在第一次運行時,這個代碼用戶永遠不會被要求獲得許可。我測試了這個代碼,結果就是我所期望的。 那麼我怎麼能要求第一次運行的權限,如果用戶先前拒絕了我的請求並做了一些事情,如果用戶拒絕我的請求並檢查不要再提問?
_「因此,與從來沒有被要求在第一次運行權限驗證碼用戶」 _'shouldShowRequestPermissionRationale'應該返回'假'如果你的應用程序尚未向用戶請求權限,那麼'else'子句將在第一次運行時執行。 – Michael
[Android M的可能重複權限:對shouldShowRequestPermissionRationale()函數的使用感到困惑(https://stackoverflow.com/questions/32347532/android-m-permissions-confused-on-the-usage-of-shouldshowrequestpermissionrati) –
@Michael但android.com認爲是不同的:如果用戶過去關閉了權限請求,並在權限請求系統對話框中選擇了「不要再請求」選項,該方法返回false。 –