英文:
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 >= 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
<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"/>
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" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论