0

i need to ask multiple permission at one time , but no dialog show in marshmallow and higher.

here is my code:

public class MainActivity extends AppCompatActivity {

private void RequestMultiplePermission() {
        // Creating String Array with Permissions.
        ActivityCompat.requestPermissions(MainActivity.this, new String[]
                {
                        READ_SMS,
                        RECEIVE_SMS,
                        READ_PHONE_STATE,
                        PROCESS_OUTGOING_CALLS,
                        INTERNET,
                        RECEIVE_BOOT_COMPLETED,
                        READ_EXTERNAL_STORAGE,
                        WRITE_EXTERNAL_STORAGE
                }, RequestPermissionCode);
    }

    // Calling override method.
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case RequestPermissionCode:
                if (grantResults.length > 0) {
                    boolean ReadSMS = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                    boolean ReceiveSms = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                    boolean phoneState = grantResults[2] == PackageManager.PERMISSION_GRANTED;
                    boolean outGoingCall = grantResults[3] == PackageManager.PERMISSION_GRANTED;
                    boolean internet = grantResults[4] == PackageManager.PERMISSION_GRANTED;
                    boolean bootComplete = grantResults[5] == PackageManager.PERMISSION_GRANTED;
                    boolean readExternalStorage = grantResults[6] == PackageManager.PERMISSION_GRANTED;
                    boolean WriteExternalStorage = grantResults[7] == PackageManager.PERMISSION_GRANTED;
                    if (ReadSMS && ReceiveSms && phoneState && outGoingCall && internet && bootComplete && readExternalStorage && WriteExternalStorage) {
                        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
                    }
                }
                break;
        }
    }

    public boolean CheckingPermissionIsEnabledOrNot() {
        int FirstPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_SMS);
        int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), RECEIVE_SMS);
        int ThirdPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_PHONE_STATE);
        int ForthPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), PROCESS_OUTGOING_CALLS);
        int FivePermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), INTERNET);
        int sixPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), RECEIVE_BOOT_COMPLETED);
        int sevenPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
        int eightPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);

        return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
                SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
                ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
                ForthPermissionResult == PackageManager.PERMISSION_GRANTED &&
                FivePermissionResult == PackageManager.PERMISSION_GRANTED &&
                sixPermissionResult == PackageManager.PERMISSION_GRANTED &&
                sevenPermissionResult == PackageManager.PERMISSION_GRANTED &&
                eightPermissionResult == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

if (Build.VERSION.SDK_INT >= 23) {
            if (!CheckingPermissionIsEnabledOrNot()) {
                RequestMultiplePermission();
                // carry on the normal flow, as the case of  permissions  granted.
            } else {
//other granted stuff
          }
     }
     }
}

result

i get no error , but dialog wasn't show , my users must manually go to app and grant permissions.

Questions

  • can anybody tell where am i wrong?

  • why we need to declare permission in manifest and another in runtime?

thanks in advance

msDead
  • 35
  • 10
  • check my answer https://stackoverflow.com/questions/40463010/how-to-add-multiple-permissions-in-the-permissions-request/40463174#40463174 – Jaiprakash Soni Nov 01 '17 at 05:09
  • There is one library you can use for runtime permission. This library are allow to use all kind of permission like single or multiple runtime permission. You can refer this like for runtime permission => "https://www.androidhive.info/2017/12/android-easy-runtime-permissions-with-dexter/ " – Yogesh Sarvaiya Oct 31 '18 at 05:55

3 Answers3

0

Your String of permissions should be like

 new String[]{
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION};

make sure you import import android.Manifest;

The permissions which you are going to ask should be also declared in manifest else dialog won't show up.

Manohar
  • 22,116
  • 9
  • 108
  • 144
0

Try this, this works as reusable and flawless for multiple permissions.

Wherever you want to ask multiple permissions use below with my reusable class:

Permissions.getPermissions(this, new String[]{
Manifest.permission.CAMERA, 
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);

My reusable multiple permission class:

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by niraj.sanghani on 10/26/2017.
 */

/**
 * Check for permissions
 *
 * {@link android.Manifest.permission}
 */
public class Permissions {

    public static void getPermissions(Context context, List<String> permissionList, int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permit(context, permissionList, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        }
    }

    public static void getPermissions(Context context, String[] permissionArray, int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permit(context, Arrays.asList(permissionArray), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private static void permit(final Context context, List<String> permissionList, final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
        final List<String> permissionsNeeded = new ArrayList<String>();

        for (String s : permissionList) {
            if (!checkPermission(context, s)) {
                permissionsNeeded.add(s);
            }
        }

        if (permissionsNeeded.size() > 0) {
            // Need Rationale
            String message = "You need to grant access ...";
            showMessageOKCancel(context, message,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            ActivityCompat.requestPermissions((Activity) context, permissionsNeeded.toArray(new String[permissionsNeeded.size()]),
                                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                        }
                    });
            return;
        }

        //return;
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private static boolean checkPermission(Context context, /*List<String> permissionsNeeded,*/ String permission) {
        if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            //permissionsNeeded.add(permission);
            // Check for Rationale Option
            if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission))
                return false;
        }
        return true;
    }

    private static void showMessageOKCancel(Context context, String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Niraj Sanghani
  • 1,493
  • 15
  • 23
0

If you haven't declared the permissions in the manifest, the permission request dialog won't be shown.

Also, if all the requested permissions have been granted earlier, the dialog won't be shown. Only permissions which haven't been granted earlier will be requested. The control will directly go to the method onRequestPermissionResult. To verify it, you can uninstall and reinstall the app and the dialog will be shown.

You need to declare the permissions in the manifest, because that's how android system was built. Before runtime permission was added, users need to grant all the permissions during the installation so that the system can display the required permissions by parsing the manifest file.

Also inside the permissions section of app information, the permissions will be displayed from the manifest.

You can use my library to handle both single and multiple runtime permissions easily:

https://github.com/nabinbhandari/Android-Permissions

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59