This article was reproduced from: http://blog.csdn.net/self_study/article/details/52965045
Recently in the study of the relevant content of authority adaptation, the collation of the previous rights blog as follows:
Android permission permissions and security mechanism resolution (on)
Android permission permissions and security mechanism analysis (next)
Android Hover window permissions for each model system adaptation Daquan
This blog is mainly about recording permissions and camera permissions to adapt, Android permission permissions and security mechanism analysis (next) I introduced in this blog post 6.0 dangerous permissions related processing steps, and the recording and camera permissions are just dangerous permissions, so need to go to separate applications, Application steps in this blog is not introduced, the above blog has been described in detail, here is mainly about how to detect the different versions of the grant or not.
Reprint Please specify source: http://blog.csdn.net/self_study/article/details/52965045.
PS: The technology is interested in the same shoe plus group 5,446,459,721 Exchange.
Recording Permissions
6.0 ago
6.0 previous detection methods my first idea is to use the Appopsmanager to detect, but the actual situation is that under the 6.0 version of the model is not accurate, so helpless found a rough solution on the Internet:
PublicClassaudiopermissioncheckutils {PrivateStaticFinal String TAG ="Audiopermissioncheckutils";Audio Get SourcePublicStaticint audiosource = MediaRecorder.AudioSource.MIC;Set the audio sample rate, 44100 is the current standard, but some devices still support 22050,16000,11025PublicStaticint samplerateinhz =44100;Set the audio's recorded channel Channel_in_stereo to two channels, Channel_configuration_mono to MonoPublicStaticint channelconfig = Audioformat.channel_in_stereo;Audio data format: PCM 16 bits per sample. ensure device support. PCM 8 bits per sample. Device support is not necessarily available.PublicStaticint audioformat = Audioformat.encoding_pcm_16bit;Buffer byte sizePublicStaticint buffersizeinbytes =0;/** * Determine if there is a recording authority */PublicStaticBooleanCheckaudiopermission (Final context context) {buffersizeinbytes =0; Buffersizeinbytes = Audiorecord.getminbuffersize (Samplerateinhz, Channelconfig, Audioformat); Audiorecord Audiorecord =New Audiorecord (Audiosource, Samplerateinhz, Channelconfig, Audioformat, buffersizeinbytes);Start Recording Audiotry{Prevent some phone crashes, such as Lenovo Audiorecord.startrecording (); }catch (IllegalStateException e) {e.printstacktrace (); AVLOGUTILS.E (TAG, log.getstacktracestring (e)); }/** * Determine if there is a recording authority based on the recording startif (audiorecord.getrecordingstate ()! = audiorecord.recordstate_recording && audiorecord.getrecordingstate () ! = audiorecord.recordstate_stopped) {AVLOGUTILS.E (TAG,"Audiorecord.getrecordingstate ()! = audiorecord.recordstate_recording:" + audiorecord.getrecordingstate ());ReturnFalse }if (audiorecord.getrecordingstate () = = audiorecord.recordstate_stopped) {If a short period of frequent detection, will cause the Audiorecord has not been destroyed, the detection will return to the recordstate_stopped state, and then read, reading will be 0 size, so at this time the default permissions throughreturn true;} byte[] bytes = new byte[1024]; int readsize = Audiorecord.read (bytes, 0, 1024); if (readsize = audiorecord.error_invalid_operation | | readsize <= 0" {avlogutils.e (TAG, "readsize illegal:" + readsize); return false;} audiorecord.stop (); AudioRecord.release (); Audiorecord = null; return true;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
This solution at a glance, is to call Audiorecord directly to the recording, if found that the recording file size is incorrect, such as 0, and then combined with the recording state at this time, you can basically determine whether the recording permission to open or not.
6.0 and after
After 6.0, the system provides the relevant API, permission detection is very simple:
int hasCameraPermission;hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);Toast.makeText(this, "camera granted : " + (hasCameraPermission == PackageManager.PERMISSION_GRANTED), Toast.LENGTH_SHORT).show();
As long as the returned value is packagemanager.permission_granted, you can determine that permission has been granted.
Camera Permissions
6.0 ago
With recording authority, versions prior to 6.0 use Appopsmanager on many models, so there is still a rough use of the following scenario:
PublicClasscamerapermissioncheckutils {Privatestatic final String TAG = " Camerapermissioncheckutils "; public static boolean checkcamerapermission (context context) {boolean canuse = true; Camera Mcamera = null; try {Mcamera = Camera.open (0); Mcamera.setdisplayorientation ( 90); } catch (Exception e) {avlogutils.e (TAG, log.getstacktracestring (e)); canuse = FALSE; } if (canuse) {mcamera.release (); Mcamera = null;} return canuse; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
But seemingly in a few models and mobile phones will still have a judgment error, and because this function will call the Camera.open function, so the less mobile phone will be a bit.
6.0 and after
After 6.0, you can use the system API directly to determine:
int hasAudioPermission;hasAudioPermission = checkSelfPermission(Manifest.permission.RECORD_AUDIO);Toast.makeText(this, "audio granted : " + (hasAudioPermission == PackageManager.PERMISSION_GRANTED), Toast.LENGTH_SHORT).show();
Similarly, you can determine that the app has been granted permission to the camera as long as the return result is packagemanager.permission_granted.
Source Download
Https://github.com/zhaozepeng/AndroidAudioCameraPermission
Copyright NOTICE: Reprint Please indicate the source Http://blog.csdn.net/self_study, the technology is interested in the same shoe plus group 5,446,459,721 Exchange
Android recording and Camera permissions adapted "Turn"