[Android Getting Started to Project Combat -- 6.1] -- How to apply for user permissions

Table of contents

1. Application authority

1. Layout file

       2. MainActivity class

3. AndroidManifest file


        You may have experienced the following scenarios when using the Android APP: When using the camera function of the APP, you need to authorize the use of the camera. So how does the APP complete the application permission function?


        Visit: https://developer.android.google.cn/reference/android/Manifest.permission.html   to view a complete list of permissions in the Android system.

1. Application authority

        First, create a new RuntimePermission project. The following is an example of applying for the CALL_PHONE permission. This permission needs to be declared when writing the call function.

1. Layout file

        Modify the activity_main.xml file, the code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/make_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打电话"/>

  </LinearLayout>

       2. MainActivity class

        Modify the MainActivity code as follows:

        The first step is to determine that the user has been authorized. With the help of the ContextCompat.checkSelfPermission() method, two parameters are received. The first is the Context, and the second is the specific permission name, and then compare the return value of the method with PackageManager.PERMISSION_GRANTED , equal means that the user is authorized, and unequal means that the user is not authorized.

        If authorized, execute the encapsulation method call() method for making calls. If not authorized, call the ActivityCompat.requestPermissions() method to apply for authorization from the user, receiving 3 parameters, the first is an instance of Activity, and the second is a String array , just put the permission name to be applied for, and the third is the request code, as long as it is a unique value.

        No matter whether it is authorized or not, it will eventually return to the onRequestPermissionsResult() method. If you agree, you can call the call() method. If you refuse, you will raise a failure prompt.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button makeCall = (Button) findViewById(R.id.make_call);
        makeCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},1);
                }else{
                    call();
                }
            }
        });
    }
    
    private void call(){
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:10086"));
            startActivity(intent);
        }catch (SecurityException e){
            e.printStackTrace();
        }
    }
    
    public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    call();
                } else {
                    Toast.makeText(this, "你拒绝的这个权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}

3. AndroidManifest file

        Modify the AndroidManifest file as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.runtimepermissiontest" >
    
    <uses-permission android:name="android.permission.CALL_PHONE"/>

    <application
............

The effect is as follows:

Guess you like

Origin blog.csdn.net/Tir_zhang/article/details/130331902
Recommended