How can I have my application ask for all permissions in MainActivity?

0

Do I want to ask for all the permits only once to make it easier for me to work?

These are my permissions

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
asked by Alex Rivas 09.04.2018 в 17:12
source

2 answers

1
  

Do I want to ask for all the permits only once to facilitate the   Is work possible?

It is possible but not recommended.

It is not necessary to add all the permissions if they are not required by your application , if you add them this would imply that when the user installs your application, a huge list of permissions was shown which possibly the user Choose not to accept them.

The manual request of the permissions is done in Android 6.0 devices and above, but it is important to know that the manual request must be made for risky permissions and not for all permissions.

  

risky permissions cover areas in which the app requires   data or resources that include private information of the user, or   that could affect the stored data of the user or the   operation of other apps. For example, the ability to read   User contacts is a risky permission. If an app declares that   needs a risky permit, the user has to grant it   explicitly the permission.

The permissions considered as risky are:

If you want to know how to make a manual permission request, there are several examples on the site:

Android manual permission request.

    
answered by 09.04.2018 / 18:58
source
0
  

Original Reply Request permissions on the MainActivity?

Add these lines to verify multiple permissions in your code:

public static final int MULTIPLE_PERMISSIONS = 100;

// función para verificar permisos
    private void checkPermission() {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.RECORD_AUDIO) + ContextCompat
            .checkSelfPermission(getActivity(),
                    Manifest.permission.WRITE_EXTERNAL_STORAGE))
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale
                (getActivity(), Manifest.permission.RECORD_AUDIO) ||
                ActivityCompat.shouldShowRequestPermissionRationale
                        (getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(
                        new String[]{Manifest.permission
                                .RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MULTIPLE_PERMISSIONS);
            }

        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(
                        new String[]{Manifest.permission
                                .RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MULTIPLE_PERMISSIONS);
            }
        }
    } else {
        //pon tu función aquí

    }
}


 //Función para iniciar después de que los permisos sean dados por el usuario
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions, int[] grantResults) {

    switch (requestCode) {
        case MULTIPLE_PERMISSIONS:
            if (grantResults.length > 0) {
                boolean recordPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                boolean writeExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                if(recordPermission && writeExternalFile)
                {
                    // Coloca tu funcion aqui
                }
            }
            else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(
                            new String[]{Manifest.permission
                                    .WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
                            MULTIPLE_PERMISSIONS);
                }
            }

    }
}
  

You can also see: Manifest.permission all the necessary permissions for an application. p>

Additional information (Translate from English):

  
    
answered by 09.04.2018 в 17:23