Android permission doesn't work even if I have declared it
(the following is extracted from a blog post of mine about this)
The big reason for not getting your permission nowadays is becauseyour project has a targetSdkVersion
of 23 or higher, and the permissionthat you are requesting is "dangerous". In Android 6.0, this includes:
ACCEPT_HANDOVER
ACCESS_BACKGROUND_LOCATION
ACCESS_MEDIA_LOCATION
ACTIVITY_RECOGNITION
ANSWER_PHONE_CALLS
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ADD_VOICEMAIL
BODY_SENSORS
CALL_PHONE
CAMERA
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
READ_CALENDAR
READ_CALL_LOG
READ_CELL_BROADCASTS
READ_CONTACTS
READ_EXTERNAL_STORAGE
READ_PHONE_STATE
READ_SMS
RECEIVE_MMS
RECEIVE_SMS
RECEIVE_WAP_PUSH
RECORD_AUDIO
SEND_SMS
USE_SIP
WRITE_CALENDAR
WRITE_CALL_LOG
WRITE_CONTACTS
WRITE_EXTERNAL_STORAGE
For these permissions, not only does your targetSdkVersion
23+ appneed to have the <uses-permission>
element(s), but you also haveto ask for those permissions at runtime from the user on Android 6.0+devices, using methods like checkSelfPermission()
andrequestPermissions()
.
As a temporary workaround, drop your targetSdkVersion
below 23.
However, eventually, you will have some reason to want yourtargetSdkVersion
to be 23 or higher. At that time, you will needto adjust your app to use the new runtime permission system.The Android documentation hasa page dedicated to this topic.
Above API level 23 you will be given programmatically pragmatically like:
private static final int PERMISSION_REQUEST_CODE = 1; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_DENIED) { Log.d("permission", "permission denied to SEND_SMS - requesting it"); String[] permissions = {Manifest.permission.SEND_SMS}; requestPermissions(permissions, PERMISSION_REQUEST_CODE); } }
request permission pragmatically (after API 23)
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted // Ask for permision ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.SEND_SMS}, 1); } else {// Permission has already been granted}
"If the app has the permission, the method checkSelfPermission() returns PERMISSION_GRANTED, and the app can proceed with the operation.
If the app does not have the permission, the method returns PERMISSION_DENIED, and the app has to explicitly ask the user for permission. You need to prompt the user for that permission, as shown in the above code. Calling requestPermissions() brings up a standard Android dialog, which you cannot customize."