When am trying to ask for permission the request dialog is appearing and the background is turning black and whatever user press "Allow" or "Deny" the app is closing and not restarting like mentioned in the documentation. This is my code
if (ActivityCompat.checkSelfPermission(Login_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Login_Activity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(Login_Activity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults)
{
switch (requestCode)
{
case 1:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//
}
else
{
//
}
break;
}
}
Activity will not restart. Once user granted permission you should call the needed function here.
After user response this method will be called.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults)
{
switch (requestCode)
{
case 1:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
// Here you should call the functions whatever you want
}
else
{
//If user didn't provide you permission. You will get this else statement. SO you should omit to call the functions(ex. If you requested for the camera access . You shouldn't call the camera functions after user denying permission. If you do app will crash)
}
break;
}
}
If you want to restart the class after user gave permission
Intent intent = getIntent();
finish();
startActivity(intent);
Call this function inside the if statement.
Related
In our android webview app, We ask for read_contacts, location, camera, audio permissions at a time on initialization of app. What happens is that simultaneously the webview url is also loaded. This causes some crucial data like location not to be passed to webview in the first load.
What we expect from the app is to load the webview url immediately after user allows, grants permissions for the above only and not before that. We tried using onRequestPermissionsResult for achieving this, but unable to do so. The code we have tried is as given hereunder
if (!check_permission(4) || !check_permission(3)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_PHONE_NUMBERS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.RECORD_AUDIO,Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
contact_perm);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
get_location();
asw_view.loadUrl(url);
}
}
}
Any help would be appreciated.
You're checking only for 0th element, i.e., grantResults[0] == PackageManager.PERMISSION_GRANTED in onRequestPermissionsResult(...).
You need to check like this:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == 1) {
// Use this method to check if all the permissions are GRANTED.
if (areAllPermissionsGranted(grantResults)) {
get_location();
asw_view.loadUrl(url);
} else {
/*
* NOTE:
* -----
* Add a Log here to check if all the permissions are granted.
* If this block doesn't executes, it means all the permissions are granted,
* something else is wrong inside your 'if' block and you need to debug that block.
* */
}
}
}
// Method to check if all the results are GRANTED.
private Boolean areAllPermissionsGranted(int[] grantResults) {
boolean isGranted = false;
if (grantResults.length > 0) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
isGranted = true;
} else {
// if a single permission is NOT_GRANTED, return from the method.
return false;
}
}
}
return isGranted;
}
Use this library
gradle:
implementation 'com.nabinbhandari.android:permissions:3.8'
then:
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; //add your requested permissions to this array
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(this/*context*/, permissions, rationale, options, new
PermissionHandler() {
#Override
public void onGranted() {
// do your task here
}
#Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied, block the feature.
}
});
While scrolling and jump onto one fragment to another, my logcat shown this error and my app crashed and restart. Please suggest me any solution regarding this?
Add in Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
In activity page
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
} else {
//TODO
}
Permissions Result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do here
} else {
Toast.makeText(mContext, "The app was not allowed to write to your storage.", Toast.LENGTH_LONG).show();
}
}
}
}
For more details, please refer the following link
http://www.theappguruz.com/blog/runtime-permissions-in-android-marshmallow
I'm trying to request permission to access the ringer using this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{ Manifest.permission.READ_CONTACTS },
REQUEST_CODE_READ_CONTACTS);
}
What I don't understand is that the code REQUEST_CODE_READ_CONTACTSis showing up with the error "cannot resolve symbol 'REQUEST_CODE_READ_CONTACTS'.
If you copied the code above from answer in this topic
then just initialize it with 1 and add this method in your code
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch (requestCode){
case REQUEST_CODE_READ_CONTACTS:
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
READ_CONTACTS_GRANTED = true;
}
}
if(READ_CONTACTS_GRANTED){
loadContacts();
}
else{
Toast.makeText(this, "Allow permission in settings", Toast.LENGTH_LONG).show();
}
}
The value is a request code that you have to define yourself.
private static final int REQUEST_CODE_READ_CONTACTS = 99; // Can be any value >= 0
You can use it to determine which permission request the user has responded to in the onRequestPermissionsResult() callback by checking for the matching value.
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_READ_CONTACTS:
// Do your thing
break;
case ...
}
}
I just need a way to get permission for using the camera of the smartphone.
I wrote this code, but the permission request dialog doesn't appear.
in the onCreate:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, 1);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// my code after granted permission
} else {
}
return;
}
}
}
requestPermissions() is not called because "ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)" is true
Step #1: Show some rationale (dialog, snackbar, something inline in your UI, whatever)
Step #2: When the user taps on something specific in that rationale UI (e.g., OK button), call requestPermissions()
Or, if you do not want to show rationale for why you are requesting the permission, do not call shouldShowRequestPermissionRationale().
So when i call requestContactPermission method, the dialog appears normally but when i click on allow button , the fragment close and the activity forced to recreate ? what seems to be the problem ? the log dose not show anything
private void requestContactPermission() {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
getLoaderManager().initLoader(0, null, this);
} else {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
101);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLoaderManager().initLoader(0, null, this);
}
break;
}
}
my main problem is when i test it in emulator its works fine but when i test it in real device, like Galaxy S7 edge i'm having this problem
Give this a try.
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Requesting Runtime Permission Read contact");
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_CONTACTS},
101);
} else {
Log.d(TAG, "Previously User have provided Read contact access");
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 101: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Write External Storage Access granted");
downloadCurrentImage();
// 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.
}
}
// other 'case' lines to check for other
// permissions this app might request
}
}