权限拒绝在Android Studio中

huangapple go评论71阅读模式
英文:

permission denial in android studio

问题

我想创建一个Android应用程序,可以在应用中查看手机联系人,并且我在我的活动中编写了以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
    requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, 1);
    requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
}
Uri u = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor curs = getContentResolver().query(u, null, null, null, null);
btn_some.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        showContacts();
    }
});

这段代码在模拟器上运行正常,但在我的手机上尝试时停止运行,Logcat消息显示如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyApplication/com.example.MyApplication.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5b66da3 28379:com.example.MyApplication/u0a264} (pid=28379, uid=10264) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

以下是我的Manifest文件中的权限声明:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

如何在我的手机上运行此应用程序?

英文:

I want to create an android application which view phone contact in application and I wrote this code in my activity

if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
            requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, 1);
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
        }
Uri u = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor curs = getContentResolver().query(u, null, null, null, null);
btn_some.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showContacts();
            }
        });

this code works fine with emulator but when I tried it on my phone it has stopped
logcat message is

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyApplication/com.example.MyApplication.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5b66da3 28379:com.example.MyApplication/u0a264} (pid=28379, uid=10264) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

and this is my Mainfest

&lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_CONTACTS&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_CONTACTS&quot;/&gt;

How can I run this application on my phone ?

答案1

得分: 1

<uses-permission android:name="android.permission.READ_CONTACTS" />

英文:

Add permission to the manifest.xml file:

<uses-permission android:name="android.permission.READ_CONTACTS" />
权限拒绝在Android Studio中

huangapple
  • 本文由 发表于 2020年8月8日 10:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63311395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定