Android Permissions Application complete resolution (a): Android comes with permission to apply

Source: Internet
Author: User

1. Why do I need permission to apply

More than 6.0 is needed, don't ask why. (Not the focus, self-search)

2. How to apply for permission
    1. Android comes with permission to apply
    2. Easypermission Permission Request

Ps:easypermission has not started to play, next time to play a blog.

3. Specific wording of permission application

3.1 Suppose there is a method that requires permission to run

    /**     * 假设这是一个需要权限才能运行的方法     */    private void PermissionTest() {        Toast.makeText(this, "这是一个需要权限才能运行的方法!", Toast.LENGTH_SHORT).show();    }

3.2 Apply for a single permission, say: Call

        button1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this, "You clicked Button1", Toast.LENGTH_SHORT).show();                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {                    // 不相等 请求授权                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);                } else {                    PermissionTest();                }            }        });

Permission to apply, you need to determine whether there is a permission, if there is, then run directly, no words in the access permission application. Individual permissions are relatively straightforward. The Activity.requestpermissions method requires three parameters, first: Context, second: incoming permission to request (multiple), Third: Request code (for callback processing)

3.2 Requesting multiple permissions

        button2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this, "You clicked Button2", Toast.LENGTH_SHORT).show();                //一次申请两个权限的写法,当然判断的时候有一个权限没有申请的时候无法执行当前方法                if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECORD_AUDIO)!= PackageManager.PERMISSION_GRANTED  ||                        ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){                    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE},2);                }                else{                    PermissionTest();                }            }        });        

Apply two permissions at a time (multiple permissions are similar, add a few more stupefied), of course, when you judge a permission does not apply when the current method cannot be executed.

ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE},2);

Incoming request permission, where to apply for recording and writing the memory card method. Request code Incoming 2

3.3 Callback Processing
    @Override public void Onrequestpermissionsresult (int requestcode, @NonNull string[] permissions, @NonNull int[] Gra Ntresults) {switch (requestcode) {case 1:if (grantresults.length > 0 && GRA                            Ntresults[0] = = packagemanager.permission_granted) {log.d (TAG, "Onrequestpermissionsresult:" +                    "Successful application of telephone permission");                    for (int i:grantresults) {log.d (TAG, "Onrequestpermissionsresult:" + i);                } permissiontest ();                } else {Toast.maketext (this, "denied the permission", Toast.length_short). Show ();            } break; Case 2:if (grantresults.length > 0 && grantresults[0] = = packagemanager.permission_granted & & Grantresults[1] = = packagemanager.permission_granted) {log.d (TAG, "onrequestpermissionsresUlt: "+" permission to apply for success ");                Permissiontest ();                } else{Toast.maketext (this, "You denied the permission", Toast.length_short). Show ();            } break;        Default:break; }    }

Description: The request code window jumps out, after the user clicks, each permission can determine whether the application succeeds.

Branch statement switch (requestcode), each case is a previously incoming request code

CASE1 Description:
A single permission has been applied, judging by the method, the value of the first item of the Grantresult array longer than the 0,grantresult array is equal to packagemanager.permission_granted (in fact it is 0, the value without authorization is equal to-1). Then execute the Permissiontest method.

CASE2 Description:
As with CASE1, first determine if the Grantresult array is longer than the 0,grantresult array of 122 is equal to packagemanager.permission_granted (because there are only two options so I write directly), The Permissiontest () method can be run after the permission request is complete.

Case2 judgment sentence can be improved: Set a isallgranted, sweep once grantrsult, just fine

    Boolean isAllGranted = true;    for (int permission : grantResults) {         if (permission != PackageManager.PERMISSION_GRANTED){            isAllGranted = false;         }    }

Ps: Tested a bit, Grantresults.length > 0 seems to be able to do without writing ... Sweat..

Conclusion

Permission to apply has been confused, after reading the book or not how to write, really to use when the heart has been afraid of. It's just a holiday today, so it took some time to write this blog. This should be my first real Android blog. The next update is easy to use.

Welcome everyone to watch, need help can leave a message. Meet is fate!

Android Permissions Application complete resolution (a): Android comes with permission to apply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.