SoFunction
Updated on 2025-03-11

Detailed explanation of Android permission query example

Detailed explanation of Android permission query example

Updated: September 30, 2022 15:31:58 Author: Chen Haonan's Nan
This article mainly introduces Android permission inquiry. This article introduces you very detailed through the example code, which has certain reference value for your study or work. Friends who need it can refer to it.

Declare permissions in

<!-- Declare all required permissions(Including ordinary permissions and dangerous permissions) -->
<uses-permission android:name=".READ_EXTERNAL_STORAGE" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".READ_CONTACTS"/>

Just put the code directly and place it where you want to intercept it. Generally, it is accessed when the program starts.

private static final int MY_PERMISSION_REQUEST_CODE = 10000;

//Step 1: Check whether there are corresponding permissions,boolean isAllGranted = checkPermissionAllGranted(
    new String[] {
        //Add to add corresponding permissions according to your needs        .READ_EXTERNAL_STORAGE,
        .WRITE_EXTERNAL_STORAGE
    }
);
// If all permissions are owned, the log file will be initialized directlyif (isAllGranted) {
    ();//What to do after you have permission    return;
}

//Step 2: Request permission// Request multiple permissions at once, and if other permissions are granted, they will be automatically ignored(
    this,
    new String[] {
        .READ_EXTERNAL_STORAGE,
        .WRITE_EXTERNAL_STORAGE
    },
    MY_PERMISSION_REQUEST_CODE
);

//Check whether you have all the permissions specifiedprivate boolean checkPermissionAllGranted(String[] permissions) {
    for (String permission : permissions) {
        if ((this, permission) != PackageManager.PERMISSION_GRANTED) {
            // As long as there is a permission not granted, it will directly return false            return false;
        }
    }
    return true;
}


//Step 3: Return to process the application permission result@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    (requestCode, permissions, grantResults);

    if (requestCode == MY_PERMISSION_REQUEST_CODE) {
        boolean isAllGranted = true;

        // Determine whether all permissions have been granted        for (int grant : grantResults) {
            if (grant != PackageManager.PERMISSION_GRANTED) {
                isAllGranted = false;
                break;
            }
        }

        if (isAllGranted) {
            // If all permissions are granted, the log file is initialized            ();//What to do after you have permission
        } else {
            // A dialog box pops up to tell the user why permissions are needed, and guides the user to manually open the permission button in the application permission management            openAppDetails();
        }
    }
}
//Open the details settings of the APPprivate void openAppDetails() {
     builder = new (this);
    ("This program needs to access "External Memory", please go to "Application Information -> Permissions" to grant it!");
    ("Go to manual authorization", new () {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent();
            (Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            (Intent.CATEGORY_DEFAULT);
            (("package:" + getPackageName()));
            (Intent.FLAG_ACTIVITY_NEW_TASK);
            (Intent.FLAG_ACTIVITY_NO_HISTORY);
            (Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            startActivity(intent);
        }
    });
    ("Cancel", null);
    ();
}

This is all about this article about Android permission inquiry. For more relevant Android permission inquiry content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

  • Android
  • Permissions
  • ask

Related Articles

  • Android APP slimming (clearing unused resources in the project) Detailed explanation

    This article mainly introduces the detailed explanation of resources not used in the Android clearance project. Here are examples to illustrate how to implement them. Friends who need it can refer to it.
    2016-11-11
  • How to get the phone's native number on Android

    This article mainly introduces the relevant information on how to obtain the mobile phone native number on Android. I hope that you can implement this method through this article. Friends who need it can refer to it.
    2017-10-10
  • Methods to replace findViewById using view binding in Android Studio 3.6

    Starting from Android Studio 3.6, view binding can replace findViewById by generating binding objects, which can help you simplify the code, remove bugs, and free it from the template code of findViewById. Today, this article will introduce to you the method of using view binding to replace findViewById. Interested friends can take a look
    2020-03-03
  • Methods of drawing various charts using achartengine in Android development

    This article mainly introduces the methods of using achartengine to draw various charts in Android development. It analyzes the specific steps and related operation techniques of Android based on the chart generation class library achartengine. If you need it, please refer to the following
    2017-10-10
  • Android realizes image scrolling effect

    This article mainly introduces the image scrolling effect of Android in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2020-09-09
  • Implementation method of parsing custom fonts in Android

    This article provides a detailed analysis and introduction to the method of using custom fonts in Android. If you need it, please refer to it
    2013-05-05
  • Android simple music playback example

    This article mainly introduces simple music playback examples of Android and introduces in detail how to use Android Service
    2015-12-12
  • Android Activity jump animation various effects sorting

    Android's Activity jump is a very stiff switching interface. In fact, Android's Activity jump can set up various animations. This article has compiled some and many animation effects, which depends on us to use our imagination.
    2013-06-06
  • How to process empty lists on Android (must-read)

    Below, the editor will bring you an article about how to deal with empty lists on Android (a must-read article). The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor
    2017-04-04
  • An example of Android ScrollView achieving rebound effect

    This article mainly introduces relevant information about the instances of Android ScrollView that achieve rebound effect. Here you can customize scrollview and achieve rebound effect. Friends who need it can refer to it
    2017-07-07

Latest Comments