I have an app that takes pictures and records audio but using separate apps (via intent) do I need to add and handle RECORD_AUDIO and CAMERA permissions in my app or it's everything in those separate apps?
It asks permission for accessing a camera app and you are open camera inside your app
You can ask permission on Runtime as below
// Requesting permission to RECORD_AUDIO
private boolean permissionToRecordAccepted = false;
private String [] permissions = {Manifest.permission.RECORD_AUDIO};
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_RECORD_AUDIO_PERMISSION:
permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
break;
}
if (!permissionToRecordAccepted ) finish();
}
and in onCreate()
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
Related
I can programmatically set, add, updated, delete APN in android 4.2.2 easily , but above android 6 I could not be able to grant WRITE_APN_SETTINGS permission using runtime permission in rooted device as a system app.
Thanks for any workaround.
Use runtime permission check if run app above 6.1 have look
int permissionCheck = ContextCompat.checkSelfPermission(YourActivity, Manifest.permission.WRITE_APN_SETTINGS);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//requesting permission
ActivityCompat.requestPermissions(YourActivity, new String[]{Manifest.permission.WRITE_APN_SETTINGS}, 1);
} else
//permission is granted
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted by user
} else
//permission denied by user
}
}
}
Hope it will help you!!
I am developing an apps where permission to write internal storage is crucial. When I am requesting permission for api >23, there is an option to never show the request permission dialog again. I wanted to disable this because if the user choose to deny and check never show the request dialog again, then the user will have to personally go to settings and clear the apps reference himself, which I want to avoid. I also want to keep requesting for permission if the user choose deny, but resulting in error java.lang.ArrayIndexOutOfBoundsException in grantResults[0].
To summarize my problem:
1. how to disable the "never show again check"
2. my error's fix
here's my code in requesting permission again if the user choose deny:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_WRITE_EXTERNAL_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Update();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_STORAGE);
}
break;
}
}
You can't stop the user from permanently denying the permission. When the user does an action that requires the permission, and the permission is denied, you can flash a message - a Toast or a Snackbar, for example.
try this my friend
String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
#Override
public void onRequestPermissionsResult ( int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(AccountClass.this, "permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AccountClass.this, "permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
I build some command line tool with android NDK and execute it in /data/local/tmp. Now it prompts me “requires android.permission.RECORD_AUDIO”. It's usually in AndroidManifest.xml for java application, but how to grant it to android shell user?
Source code like:
sp<AudioRecord> rec = new AudioRecord(AUDIO_SOURCE_MIC, 44100, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO);
Logcat:
I/AudioFlinger( 3772): AudioFlinger's thread 0xf0e838c0 ready to run
W/ServiceManager( 3772): Permission failure:android.permission.RECORD_AUDIO from uid=2000 pid=-1
E/ ( 3772): Request requires android.permission.RECORD_AUDIO
E/AudioFlinger( 3772): openRecord() permission denied: recording not allowed
E/AudioRecord(14132): AudioFlinger could not create record track, status: -1
--------- beginning of crash
...
After Lollipop, Android platform is using real time permissions. If your app running in Marshmallow and after versions, you should grant your permissions in Activity.
Before request permissions:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED) {
// put your code for Version>=Marshmallow
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(this,
"App required access to audio", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}
} else {
// put your code for Version < Marshmallow
}
After this, overraide this method and put your code:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_AUDIO_PERMISSION_RESULT) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"Application will not have audio on record", Toast.LENGTH_SHORT).show();
}
}
}
private void requestRecordAudioPermission() {
String requiredPermission = Manifest.permission.RECORD_AUDIO;
// If the user previously denied this permission then show a message explaining why
// this permission is needed
if (getActivity().checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(getActivity(), "This app needs to record audio through the microphone....", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{requiredPermission}, 101);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == 101 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// This method is called when the permissions are given
}
}
Execute this code if you have device with marshmallow or upper OS
I have an application which ask for permission from the user to start the device's bluetooth. I want to know whether if the user accepted or denied the permission.
Is there any method to do that.
You must override onRequestPermissionsResult method.
For example, here you check if user granted permision for access to his location:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//Do something here
}
}
}
}
Also, before that when you ask your permission you must determine your requestCode, which will be also sent to this method, when the user will be asked for permission. So it would be something like here:
if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
So now, my requestCode is equal to 1(and i am checking that later in my onActivityRestult method.
Hope this helps.
I am to new to Android 6.0 Coding Please Provide a solutions For the Below Code:
When i provide Run Time Permissions like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE it shows an Exception like
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Pictures/MyAppNew%20File%20Upload/IMG_20160401_110234.jpg} (has extras) } from ProcessRecord{ed96564 26955:com.social.nocializer/u0a259} (pid=26955, uid=10259) with revoked permission android.permission.CAMERA
Either MediaStore.ACTION_IMAGE_CAPTURE and MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE Run Time Permissions are not working...
Note: READ_EXTERNAL_STORAGE Works For When Opening Gallery
You have to manage run time permission for this, Because whichever permissions you have defined in AndroidManifest will not be automatically granted. So like below method you can check whether you permission is approved or not
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
}
Here, MY_REQUEST_CODE is a static constant that you can define, which will be used again for the requestPermission dialog callback. Now, You will need a callback for the dialog result:
#Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}
}
#Ronak Solution worked for me but with a few following changes, As we need to check on only those devices which are above Android M.
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
And override the following method using crl+o copying, pasting would might result in an error :D
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}