I am not sure how to do this at runtime without a user request. However, you indicated in the header that it is intended for testing. Therefore, I have some options that I can offer you
a) When testing needs to be performed without permissions, simply cancel it manually using your device’s settings.
b) You can check and ask for permission, and then refuse to give permission. I would give you the code that I use to check and request camera permission. You just need to change the permission name and possibly the condition to check:
public static boolean checkCameraPermission(MainActivity thisActivity) { return ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED; } public static void checkAndAskCameraPermission(final MainActivity thisActivity) { if (!checkCameraPermission(thisActivity)) {
I could give you some more links about b) (how to ask permission) if you need it (just comment on this). However, since I am not sure that this answer would interest you, I will not waste time documenting this option.
But then, maybe someone can explain to you how to do this directly at runtime without a hint, but I, although it may be interesting to provide you with at least this information.
Feuby
source share