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 because
your project has a targetSdkVersion
of 23 or higher, and the permission
that 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+ app
need to have the <uses-permission>
element(s), but you also have
to 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 need
to adjust your app to use the new runtime permission system.
The Android documentation has
a page dedicated to this topic.
Permission denial exception, even though correct permissions have been added to the Manifest?
Prior to Android Marshmallow, declaring the permissions in manifest was enough and system used to grant those permissions during installation. But starting from Android M declaring the permissions is not enough. The authority to grant the permission has been transferred to user. You need to request those permissions at runtime in order to allow users to grant the permissions.
Follow this official documentation for implementation details.
Permissions in manifest doesn't work
Maybe your using android OS version above Kitkat. where you have to give permissions programmatically.
see below mentioned link
https://developer.android.com/training/permissions/requesting.html
Android runtime permission passes even if it doesn't have permission
Remove following check from your if condition:
ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET)!= PackageManager.PERMISSION_GRANTED
The above condition will be always evaluate to false
since Internet permission is classified as normal permission and is granted at installation time.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
Toast.makeText(this, "This app needs camera and storage permissions", Toast.LENGTH_LONG).show();
}else{
onCreateAfterPermission();
}
}
One more thing, you should segregate the checking for CAMERA
& WRITE_EXTERNAL_STORAGE
permission. This is because, there could be case where CAMERA
permission is granted but WRITE_EXTERNAL_STORAGE
permission is not granted.
With your checking the condition will not satisfy hence WRITE_EXTERNAL_STORAGE
wont be asked.
Check out this SO for implementation details for better approach.
Do we need to explicitly ask for the permission other than AndroidManifest.xml in Marshmallow?
In Android M and above, you have to ask for the permissions that are classified as "dangerous". You can find a full list of the permissions that needs to be requested here.
You can, however, avoid requesting by setting compileSDK and targetSDK to < 23. Note that this prevents you from using any of the API 23+ features.
You request the permissions like this:
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},//Your permissions here
1);//Random request code
Check if the user is running API 23+ by doing:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//Request: Use a method or add the permission asking directly into here.
}
And if you need to check the result, you can do it like this:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// 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.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
permission.WRITE_CALENDAR: no permission though declared in the manifest.xml
Make sure that Your project has a targetSdkVersion
of 23 or higher
Try this
ask permission like this
ActivityCompat.requestPermissions(Activity.this,
new String[]{Manifest.permission.WRITE_CALENDAR},
1);// pass here permission request code to handle permission result
than handle permission results in onRequestPermissionsResult
to know user has allowed permission or not
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
c.addEvToCal();
// 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;
}
}
}
Related Topics
Convert Word Doc to HTML Programmatically in Java
How to Save the Current Date/Time When I Add New Value to Firebase Realtime Database
How to Get Current Location in Android
Java Can't Connect to X11 Window Server Using 'Localhost:10.0' as the Value of the Display Variable
Understanding Checked VS Unchecked Exceptions in Java
Difference Between Jsf, Servlet and Jsp
Why Is It Bad Practice to Call System.Gc()
Example Images For Code and Mark-Up Q&As
Singletons Vs. Application Context in Android
Concurrent Modification Exception: Adding to an Arraylist
Difference Between Chromedriver and Webdriver in Selenium
Java.Util.Nosuchelementexception - Scanner Reading User Input