我正在尝试访问联系人并显示它们,但它不会让我,它拒绝我访问...我已经阅读了本网站中的其他答案但仍然有错误...我已经给了权限正确。
这是代码:
TextView contactView = (TextView) findViewById(R.id.contactview);
Cursor cursor = getContacts();
while(cursor.moveToNext()){
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
contactView.append("Name: ");
contactView.append(displayName);
contactView.append("\n");
}
//End of onCreate
private Cursor getContacts(){
//runs the query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {ContactsContract.Contacts._ID ,ContactsContract.Contacts.DISPLAY_NAME};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
}
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pc.contentprovider">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ContactsActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
日志:
01-17 23:12:33.250 10142-10142/com.example.pc.contentprovider E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pc.contentprovider, PID: 10142 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pc.contentprovider/com.example.pc.contentprovider.ContactsActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5150ff8 10142:com.example.pc.contentprovider/u0a57} (pid=10142, uid=10057) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5150ff8 10142:com.example.pc.contentprovider/u0a57} (pid=10142, uid=10057) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
at android.content.ContentResolver.query(ContentResolver.java:475)
at android.content.ContentResolver.query(ContentResolver.java:434)
at com.example.pc.contentprovider.ContactsActivity.getContacts(ContactsActivity.java:60)
at com.example.pc.contentprovider.ContactsActivity.onCreate(ContactsActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-17 23:13:12.640 10142-10142/? I/Process: Sending signal. PID: 10142 SIG: 9
答案 0 :(得分:2)
假设您使用的是Android Studio,请检查您的gradle.build文件并查看您要定位的Android版本。
如果是23,那么您需要使用新的Marshmallow权限系统。
如果您将TARGET设置为21并降低它将使用现在较旧的清单声明权限系统,该系统会立即向应用程序安装用户请求所有权限。这也适用于Android 6设备。
我鼓励你去做前者而不是后者,因为这是申请权限的新方法。
答案 1 :(得分:1)
这是什么版本的Android运行? 在Android 6.0中,您必须在代码中请求权限。 您可以检查这是在手机设置中查找此应用程序的问题,并为其提供手动所需的权限。
答案 2 :(得分:0)
使用Android 6.0,权限模型已更改,现在您需要在运行时请求用户获得权限。请查看此Google示例,其中说明了如何申请联系人权限here。请仔细阅读此开发人员说明here。
如果您的Android API版本低于23,那么您列出的旧版本可以使用。对于高于和等于API版本23的任何内容,您需要在运行时请求权限。
从API版本23定义了两种类型的权限,一种使用PROTECTION_NORMAL,另一种不使用。 PROTECTION_NORMAL权限仍然可以使用旧方式访问网络状态和Internet,这样做不会造成很大的危害。查看here
答案 3 :(得分:0)
您需要使用newer versions of android(6.0 +)的新权限。
以下是该网站的一些示例:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}