Android determines whether the user has allowed the camera permission instance code

For example, since it is to judge whether the user has allowed the camera permission, we will ignore the problem of manifest configuration, because it is the business of the developer.

When users use the app, if they enter the place where the camera is used for the first time, the mobile phone will prompt whether to allow the application to use the camera. Some users shake their hands or don't want to turn on the camera at all. Click to turn it off. That's good. The next time you enter this function, the app will be black or crash.

As a developer, the normal idea is to prompt the user that the camera permission is turned off by you. Hurry to turn it on manually, otherwise, you won't want to use this function! So, how can we realize this idea?

1、 Judge camera permissions

The Android API does not provide a method to judge whether the camera permission is turned off, but we still have a way to directly access the code:

The above method, rest assured copy to your code, before using the camera, call back, return to ture indicates that the camera privileges are open. On the contrary, the camera permission is turned off by the user.

note appended:

1. It can be seen that the idea of code implementation is to call camera. Open (), and then judge the permission status according to whether there is an exception. Interested students can take a look at the android.hardware.camera class in the Android SDK. This is what Google does in line 490 or so.

2. Because the ROM of Meizu mx5 has changed a lot of things, the method of big brother Google can not be used directly, so the code with comments above is added. There is no too much explanation here.

2、 Give a hint

If the camera permission is turned off by the user, the processing idea is: a dialog box will pop up to prompt the user to go to the system settings, find the camera management permission interface, and manually open the camera permission.

3、 Jump to the setting interface and manually open the permission

The code of jump setting interface is as follows:

Note: some people will ask, why not directly jump to the camera permission management interface, and just change an action? Yes, this idea is right, but the actions of different manufacturers to open the camera permission management interface are different. The difference is too big. I tried and couldn't move... Interested students can try Oh, verify it

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>