Reputation: 55
I want to make an app which asks user for two permissions- READ_CALL_LOG and READ_CONTACTS. Now if the user denies READ_CALL_LOG then it should show a warning which should have a button that asks the user to grant permission again. When that button is clicked the user is asked again to grant READ_CALL_LOG permission. When the user grants READ_CALL_LOG permission he is taken to READ_CONTACTS permission. If he denies READ_CONTACTS permission then again it should show a warning which should have a button that asks the user to grant permission again. When that button is clicked the user is asked again to grant READ_CONTACTS permission. I don't want the user to be navigated to Settings if he denies. I have tried this code:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*if(((ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CALL_LOG)== PackageManager.PERMISSION_DENIED)&&(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS)== PackageManager.PERMISSION_DENIED))||((ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CALL_LOG)!= PackageManager.PERMISSION_GRANTED)&&(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED)))
requestPermission();*/
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CALL_LOG)== PackageManager.PERMISSION_DENIED)
{
requestCallLogsPermission();
}
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS)== PackageManager.PERMISSION_DENIED)
{
requestContactPermission();
}
}
private void requestCallLogsPermission()
{
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CALL_LOG)!= PackageManager.PERMISSION_GRANTED)
{
Dexter.withActivity(MainActivity.this).withPermission(Manifest.permission.READ_CALL_LOG).withListener(new PermissionListener()
{
@Override
public void onPermissionGranted(PermissionGrantedResponse response)
{
Toast.makeText(MainActivity.this, "Call Logs Permission is granted !", Toast.LENGTH_SHORT).show();
//if((ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS)== PackageManager.PERMISSION_DENIED)||ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED)
requestContactPermission();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Permissions");
builder.setMessage("Permission is required");
builder.setCancelable(false);
builder.setPositiveButton("Understood, I am ready to give the permissions required", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
requestCallLogsPermission();
}
}).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
}
}
private void requestContactPermission()
{
Dexter.withActivity(MainActivity.this).withPermission(Manifest.permission.READ_CONTACTS).withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
Toast.makeText(MainActivity.this, "Read Contacts Permission is granted !", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Permissions");
builder.setMessage("Permission is required");
builder.setCancelable(false);
builder.setPositiveButton("Understood, I am ready to give the permissions required", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestCallLogsPermission();
}
}).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
}
}
Upvotes: 1
Views: 63
Reputation: 3576
You can try this method:
1.) Setting up permissions:
String[] permissions = {
android.Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
}; // Use the permission you need
2.) -Check and request if not already permitted:
onCreate() {
if (!checkPermissions()){
ActivityCompat.requestPermissions(this, permissions, 1);
}
}
private boolean checkPermissions() {
for (String permission: permissions){
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
3.) Check for denial status and keep asking till all permissions are granted:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean allGranted = true;
for (int result: grantResults){
if (result == PackageManager.PERMISSION_DENIED){
allGranted = false;
}
}
if (!allGranted){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) ||
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE))
{
// Here do whatever you want once the user denies
final String[] mPermissions = permissions;
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert!");
alertDialog.setMessage("Please allow the permissions!");
alertDialog.setCancelable(false);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((MainActivity.this), mPermissions, 1);
dialog.dismiss();
}
});
alertDialog.show();
}
else {
Log.d(TAG, "PERMANENTLY DENIED");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert!");
alertDialog.setMessage("All permissions are necessary for app to run. Goto settings and grant them.");
alertDialog.setCancelable(false);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
dialog.dismiss();
}
});
alertDialog.show();
}
}
}
Upvotes: 1