android studio api 23 android 6.0 requires android.permission.READ_CONTACTS or android.permiss

来源:互联网 发布:网络诈骗主题班会记录 编辑:程序博客网 时间:2024/06/14 10:03
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{49af1fb 14363:com.example.gszczepanski.eduandroid/u0a59} (pid=14363, uid=10059) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
在manifest 文件中已经添加了user-permission 
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<span style="color:#2e3133;">但还是报了前面出现的错误</span><pre class="default prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 1em; padding: 5px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; font-size: 13px; width: auto; max-height: 600px; overflow: auto; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; word-wrap: normal; background-color: rgb(238, 238, 238);"><code style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span style="color:#ff0000;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">requires android</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">permission</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">READ_CONTACTS </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">or</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;"> android</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">permission</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;">WRITE_CONTACTS</span></span></code>
这时,应该考虑的是android机制的问题,可以看看(Requesting Permissions at Run Time)
错误提示的意思是:需要用户自己去添加对contacts 的授权,而不是让系统给授权,这都是android 6.0及以上的设备上才出现的问题,google 主要是处于安全的考虑,才添加的机制;
解决的方法是:弹出授权提示框,提醒用户授权;
代码如下:
 if (ActivityCompat.checkSelfPermission(this, mPermission[0])                != MockPackageManager.PERMISSION_GRANTED ||                ActivityCompat.checkSelfPermission(this, mPermission[1])                        != MockPackageManager.PERMISSION_GRANTED                )
//授权(如果没有授权,就弹出提示,让用户授权)            ActivityCompat.requestPermissions(this,                    mPermission, REQUEST_CONTACTS);
希望对你有帮助;


1 0