1

I am trying to add permission in my project for marshmallow user the situation is:
I need 3 permissions for my device Camera, SMS, Phone Calls

When application runs only one permission is shown and the other two permissions got skipped ,

and I close my app and re-open it it shows second permission and the 3rd one skipped, and closed it again and opened it the 3rd permission is shown so on.

While trying it on Emulator it works perfectly all 3 permissions are shown at once but not in Device(i.e Samsung j5, Samsung j7):

Permission.Java

    public class PermissionActivity extends Activity {
    int count = 0;
    public static final String CAMERA_PREF = "camera_pref";
    public static final String SMS_PREF = "sms_pref";
    public static final String CALL_PREF = "call_pref";


    public static final int MY_PERMISSIONS_REQUEST_CAMERA = 100;
    public static final int MY_PERMISSIONS_REQUEST_RECIEVE_SMS = 101;
    public static final int MY_PERMISSIONS_REQUEST_PHONE = 102;


    public static final String ALLOW_KEY = "ALLOWED";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;


        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {

            chk_all_permissions();

        } else {

            // do something for phones running an SDK before lollipop
            start_app();

        }


    }

    // Example for sms permissions
    public static Boolean getFromPref(Context context, String key) {
        SharedPreferences myPrefs = context.getSharedPreferences(CAMERA_PREF,
                Context.MODE_PRIVATE);
        Log.e("Key", String.valueOf(key));
        return (myPrefs.getBoolean(key, false));
    }
    public static Boolean getFromPrefSms(Context context, String key) {
        SharedPreferences myPrefs = context.getSharedPreferences(SMS_PREF,
                Context.MODE_PRIVATE);
        Log.e("Key", String.valueOf(key));
        return (myPrefs.getBoolean(key, false));
    }
    public static Boolean getFromPrefCalls(Context context, String key) {
        SharedPreferences myPrefs = context.getSharedPreferences(CALL_PREF,
                Context.MODE_PRIVATE);
        Log.e("Key", String.valueOf(key));
        return (myPrefs.getBoolean(key, false));
    }

    private void chk_all_permissions() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.RECEIVE_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            marshmallow_permissions_sms();
        } else if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {

            marshmallow_permission_camera();
        } else if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            marshmallow_permission_phone();

        }
    }


    // Example for camera permissions
    private void marshmallow_permission_camera() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            if (getFromPref(this, ALLOW_KEY)) {
                customPermDialogue();

            } else if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                Log.e("Key", "CAMERA");

                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CAMERA)) {
                    ActivityCompat.requestPermissions(PermissionActivity.this,
                            new String[]{Manifest.permission.CAMERA,},
                            MY_PERMISSIONS_REQUEST_CAMERA);
                    Log.e("Key5", "SCAmera");

                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.CAMERA,},
                            MY_PERMISSIONS_REQUEST_CAMERA);
                    Log.e("Key2", "SCAmera");


                }
            }
        }
    }



    private void marshmallow_permissions_sms() {

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.RECEIVE_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            if (getFromPrefSms(this, ALLOW_KEY)) {
                Log.e("Key", "SMS2");

                customPermDialogue();

            } else if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.RECEIVE_SMS)
                    != PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                Log.e("Key", "SMS");

                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.RECEIVE_SMS)) {
                    ActivityCompat.requestPermissions(PermissionActivity.this,
                            new String[]{Manifest.permission.RECEIVE_SMS,},
                            MY_PERMISSIONS_REQUEST_RECIEVE_SMS);
                    Log.e("Key5", "SMS");



                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.RECEIVE_SMS,},
                            MY_PERMISSIONS_REQUEST_RECIEVE_SMS);
                    Log.e("Key2", "SMS");



                }
            }
        }


    }


// Example for phone permissions


private  void marshmallow_permission_phone(){
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            if (getFromPrefCalls(this, ALLOW_KEY)) {
                Log.e("Key", "CALL2");

                showSettingsAlert();

            } else if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                Log.e("Key", "CALL");

                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.READ_PHONE_STATE)) {
                    ActivityCompat.requestPermissions(PermissionActivity.this,
                            new String[]{Manifest.permission.READ_PHONE_STATE,},
                            MY_PERMISSIONS_REQUEST_PHONE);
                    Log.e("Key5", "PHONE");


                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.READ_PHONE_STATE,},
                            MY_PERMISSIONS_REQUEST_PHONE);
                    Log.e("Key2", "CALL");


                }
            }
        }

    }



    public void showSettingsAlert() {
        AlertDialog alertDialog = new AlertDialog.Builder(PermissionActivity.this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("App needs to access the Camera.");

        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "DONT ALLOW",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ALLOWAnce",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        chk_all_permissions();
                        dialog.dismiss();

                    }
                });
        alertDialog.show();

    }

    // A dialogue to show if permission is declined
    private void customPermDialogue() {
        AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
        alertDialog.setTitle("Alert");
        alertDialog.setCancelable(false);
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setMessage("App needs to access required Permissions !");
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        chk_all_permissions();
                    }
                });
        alertDialog.show();
    }
    public void start_app()
    {
        Intent i=new Intent(this,SplashScreen.class);
        startActivity(i);

    }
    public void showCustomAlert(String msg)
    {

        Toast toast = Toast.makeText(getApplicationContext(),
                msg,
                Toast.LENGTH_SHORT);
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        v.setTextColor(Color.parseColor("#25456d"));
        View toastView = toast.getView();
        toastView.setBackgroundResource(R.drawable.toastdrawable);
        toast.show();

    }

Mainifest.xml

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
Kumail Hussain
  • 869
  • 2
  • 26
  • 49

2 Answers2

0

That's because you are checking for permissions in if-else if statements in which effectively only one satisfied condition can be executed at a time:

if (true) {
    // only this one is executed
} else if (true) {
    // does not execute
} else if (true) {
    // does not execute
}

Also I would recommend simplifying your code because your problems are effect of overcomplicating. Here is an example of how you should ask for multiple permissions: Android 6.0 multiple permissions

// edit: After rereading your question, I noticed you also asked about different behaviour between device and emulator. It's probably because you have different saved preferences and allowed preferences in both environments. Please remove app data and reset app permissions both on your device and emulator and then check again.

Community
  • 1
  • 1
Rafal Zawadzki
  • 963
  • 6
  • 15
0

i have done it by using three different activities and worked perfectly and cheap way but have a solution:

Proper implementation with alert boxes also checked if user check
never ask again

PemrissionCamer.java

    import android.Manifest;
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.provider.Settings;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.internal.view.ContextThemeWrapper;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;

    public class PermissionCamera extends Activity {
          static int count=0;
        int alert_time=0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_permissioncalls);
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;


            if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(PermissionCamera.this,
                            new String[]{Manifest.permission.CAMERA},
                            3);
                } else {
               start_app();
                }
            }
            else {
                start_app2();
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {

                case 3: {

                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        count+=1;

                        if(count==1)
                        {
                            start_app();


                        }
                        // permission was granted, yay! Do the
                        // contacts-related task you need to do.
                    } else {
                        Log.e("Camera3","Camera2");


                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
                        showAlert();

                    }
                    return;
                }
                // other 'case' lines to check for other
                // permissions this app might request
            }
        }
        public void start_app()
        {
            Intent i=new Intent(this,PermissionSms.class);
            startActivity(i);

        }
        public void start_app2()
        {
            Intent i=new Intent(this,SplashScreen.class);
            startActivity(i);

        }
        private void showAlert() {

            AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
            alertDialog.setTitle("Alert");
            alertDialog.setCancelable(false);
            alertDialog.setIcon(R.drawable.icon);
            alertDialog.setMessage("App needs to access required Permissions !");
            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            quit();

                            dialog.dismiss();
                            finish();
                        }
                    });
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            boolean showRationale = shouldShowRequestPermissionRationale( Manifest.permission.CAMERA );
                            if((! showRationale))
                            {
                                Log.e("Camera2","Camera2");
                                showAlert2();
                            }
                           else  if (ContextCompat.checkSelfPermission(PermissionCamera.this,
                                    Manifest.permission.CAMERA)
                                    != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions(PermissionCamera.this,
                                        new String[]{Manifest.permission.CAMERA},
                                        3);
                                dialog.dismiss();
                            }
                        }
                    });

            alertDialog.show();



    }
        private void showAlert2() {
            final String package_app="com.gwnt.flashlight.alert.call.and.sms";
            AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
            alertDialog.setTitle("Alert");
            alertDialog.setCancelable(false);
            alertDialog.setIcon(R.drawable.icon);
            alertDialog.setMessage("You have checked 'Never ask again' enable permission manually!");

            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          quit();

                        }
                    });
            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", package_app, null);
                            intent.setData(uri);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                           // quit();
                             dialog.dismiss();
                        }
                    });

            alertDialog.show();



        }
        public void quit()
        {
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            finish();
            PermissionCamera.this.finish();
            System.exit(0);

        }
    }

PermissionSms.Java

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.internal.view.ContextThemeWrapper;
import android.view.Window;
import android.view.WindowManager;

import java.security.Permission;

public class PermissionSms extends Activity {
    static int count=0;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_permissioncalls);

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.RECEIVE_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(PermissionSms.this,
                    new String[]{Manifest.permission.RECEIVE_SMS},
                    1);
        } else {
          start_app();

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {

            case 1: {

                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    count+=1;
                    if(count==1)
                    {

                        start_app();


                    }
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                } else {
                        showAlert();
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.

                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    public void start_app()
    {
        Intent i=new Intent(this,PermisiionCall.class);
        startActivity(i);

    }
    private void showAlert() {
        AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
        alertDialog.setTitle("Alert!!");
        alertDialog.setCancelable(false);
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setMessage("App needs to access required Permissions !");
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        quit();
                        dialog.dismiss();
                        finish();
                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        boolean showRationale = shouldShowRequestPermissionRationale( Manifest.permission.RECEIVE_SMS );
                        if((! showRationale))
                        {
                            showAlert2();
                        }
                        if (ContextCompat.checkSelfPermission(PermissionSms.this,
                                Manifest.permission.RECEIVE_SMS)
                                != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(PermissionSms.this,
                                    new String[]{Manifest.permission.RECEIVE_SMS},
                                    1);
                            dialog.dismiss();
                        }
                    }
                });

        alertDialog.show();



    }
    private void showAlert2() {
        final String package_app="com.gwnt.flashlight.alert.call.and.sms";
        AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
        alertDialog.setTitle("Alert");
        alertDialog.setCancelable(false);
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setMessage("You have checked 'Never ask again' enable permission manually!");

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        quit();

                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", package_app, null);
                        intent.setData(uri);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        // quit();
                        dialog.dismiss();
                    }
                });

        alertDialog.show();



    }
    public void quit()
    {
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
        finish();
        PermissionSms.this.finish();
        System.exit(0);

    }
}

PermissionReceivePhone.java

    package com.gwnt.flashlight.alert.call.and.sms;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.internal.view.ContextThemeWrapper;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class PermisiionCall extends Activity {
    static int count=0;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_permissioncalls);

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(PermisiionCall.this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    2);
        } else {
            start_app();

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {

            case 2: {

                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    count+=1;
                    if(count==1)
                    {

                        start_app();


                    }
                    // 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.
                    showAlert();

                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    public void start_app()
    {
        Intent i=new Intent(this,SplashScreen.class);
        startActivity(i);

    }
    private void showAlert() {
        AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
        alertDialog.setTitle("Alert!!");
        alertDialog.setCancelable(false);
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setMessage("App needs to access required Permissions !");
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        quit();

                        dialog.dismiss();
                        finish();
                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        boolean showRationale = shouldShowRequestPermissionRationale( Manifest.permission.READ_PHONE_STATE );
                        if((! showRationale))
                        {
                            showAlert2();
                        }
                        else if (ContextCompat.checkSelfPermission(PermisiionCall.this,
                                Manifest.permission.READ_PHONE_STATE)
                                != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(PermisiionCall.this,
                                    new String[]{Manifest.permission.READ_PHONE_STATE},
                                    2);
                            dialog.dismiss();
                        }
                    }
                });

        alertDialog.show();



    }
    private void showAlert2() {
        final String package_app="com.gwnt.flashlight.alert.call.and.sms";
        AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
        alertDialog.setTitle("Alert");
        alertDialog.setCancelable(false);
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setMessage("You have checked 'Never ask again' enable permission manually!");

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        quit();

                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", package_app, null);
                        intent.setData(uri);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        // quit();
                        dialog.dismiss();
                    }
                });

        alertDialog.show();



    }
    public void quit()
    {
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
        finish();
        PermisiionCall.this.finish();
        System.exit(0);

    }
}

It will first ask camera permission if permitted it will go for sms permission if permitted it will go for phone

startapp is function to move to the next activity if permission is allowed
i.e camera->sms->phone->functionalities

Kumail Hussain
  • 869
  • 2
  • 26
  • 49