8

My project is a long running project. I had set the target version as 10, 4 years back. I cant change the target version to 23, since I am using httpImageCache and also having issues with UI's. My problem is, when Marshmallow released I tried to integrate Marshmallow with targetVersion 10,

 int returnedPermission = ContextCompat.checkSelfPermission(MyActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

this function is always returing '0' if I manually ON or Off storage permission from App Settings page. Can any one please help me?

rciovati
  • 27,603
  • 6
  • 82
  • 101
neena
  • 551
  • 1
  • 8
  • 24
  • Can you try the non compat version of that method, 'Context.checkSelfPermission' ? 0 is the constant 'PackageManager.PERMISSION_GRANTED'. Should return 'PackageManager.PERMISSION_DENIED' (-1) if not granted. – Mister Smith Oct 20 '15 at 09:41
  • yes, that what I want, when I OFF from App settings it should return -1, but this function always returning 0. – neena Oct 20 '15 at 09:42
  • It might be a bug in the compatibility library. Try the standard version, and check if the results are the same. – Mister Smith Oct 20 '15 at 09:44
  • There is no such method as ''Context.checkSelfPermission'' – neena Oct 20 '15 at 09:49
  • @Mister Smith, could you please explain that further? – neena Oct 20 '15 at 09:50
  • Of course there is. Check the docs [here](http://developer.android.com/reference/android/content/Context.html#checkSelfPermission%28java.lang.String%29). It was added in API 23, so you can only use it in Marshmallow devices/emulators. This is not compatible with previous versions, but for a quick test it is ok. – Mister Smith Oct 20 '15 at 09:52
  • There is an answer on your problem: http://stackoverflow.com/questions/33407250/checkselfpermission-method-is-not-working-in-targetsdkversion-22 – MikePtr Apr 29 '16 at 07:13

3 Answers3

4

As @Commonware has already given the answer, but here I am adding more detail to the question which might help you. As per the official android developer site:

  • If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all.

  • If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

As your target SDK is 10, application will run perfectly like previous. Anyway please note that user still can revoke a permission after that..!!! Although Android 6.0 warn the user when they try to do that but they can revoke anyway.

Above statement is taken from official android developer site.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
  • So, if the targetVersion is lower than 23 and if the user revoke permissions, I cant do anything, is that right? – neena Oct 20 '15 at 12:37
  • When we call a function that requires a permission user revoked on application with targetSdkVersion less than 23, Any Exception will not be thrown. Instead it will just simply do nothing. For the function that return value, it will return either null or 0 depends on the case. Although application would not be crashed from calling a function. It may still can crash from what that application does next with those returned value. – Dhaval Patel Oct 20 '15 at 12:42
3

Can any one please help me?

Delete that code, as it is useless for you. If your targetSdkVersion is below 23, you cannot find out whether or not the user revoked permissions.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So I cant do anything when the user turn OFF the Permissions. – neena Oct 20 '15 at 12:36
  • @neena: Correct. You should not get any `SecurityExceptions` if the user revokes permissions, but the APIs protected by those permissions should return empty results (e.g., no GPS fixes, no contacts). – CommonsWare Oct 20 '15 at 12:37
3

use PermissionChecker.checkSelfPermission()

when targetSdkVersion <= 22,you also can use requestPermission()

Community
  • 1
  • 1
qinmiao
  • 5,559
  • 5
  • 36
  • 39
  • This is the correct answer. I'm guessing PermissionChecker was made available some time after @commonsware posted his answer – vaughandroid Jul 04 '16 at 12:31