Android中联系人的权限拒绝
我正在尝试访问联系人并显示它们,但它不允许我访问,它拒绝我访问…我已经阅读了此网站中的其他回复,但仍然存在错误…我已经以正确的方式授予了权限 代码如下:Android中联系人的权限拒绝,android,Android,我正在尝试访问联系人并显示它们,但它不允许我访问,它拒绝我访问…我已经阅读了此网站中的其他回复,但仍然存在错误…我已经以正确的方式授予了权限 代码如下: TextView contactView = (TextView) findViewById(R.id.contactview); Cursor cursor = getContacts(); while(cursor.moveToNext()){ String displayName = curso
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>
这是在什么版本的Android上运行的? 在Android 6.0中,您必须在代码中请求权限。
您可以检查在“手机设置”中查找此应用程序并手动授予其所需权限是否存在问题。此应用程序运行在哪个版本的Android上? 在Android 6.0中,您必须在代码中请求权限。
您可以检查在手机设置中查找此应用程序并手动授予其所需权限是否存在问题。如果您使用的是Android Studio,请检查gradle.build文件,查看您的目标Android版本 如果是23,则需要使用新的棉花糖权限系统 如果将目标设置为21或更低,它将使用现在较旧的清单声明权限系统,该系统会要求应用程序安装的用户立即获得所有权限。这也适用于Android 6设备
我鼓励您使用前者而不是后者,因为这是请求权限的较新方法。假设您使用的是Android Studio,请检查gradle.build文件,查看您的目标Android版本 如果是23,则需要使用新的棉花糖权限系统 如果将目标设置为21或更低,它将使用现在较旧的清单声明权限系统,该系统会要求应用程序安装的用户立即获得所有权限。这也适用于Android 6设备
我鼓励您使用前者而不是后者,因为这是请求权限的较新方法。在Android 6.0中,权限模型已经更改,现在您需要在运行时请求用户的权限。看看这个google示例,它解释了如何请求联系人权限。通读此开发人员说明 如果您运行的Android API版本低于23,那么您列出的旧方法可以工作。对于API版本23以上的任何内容,您需要在运行时请求权限
API版本23定义了两种类型的权限,一种具有保护功能,另一种不具有保护功能。正常权限的保护仍然以旧的方式工作,例如访问网络状态和Internet,这不会造成太大危害。检查Android 6.0的权限模型已更改,现在需要在运行时向用户请求权限。看看这个google示例,它解释了如何请求联系人权限。通读此开发人员说明 如果您运行的Android API版本低于23,那么您列出的旧方法可以工作。对于API版本23以上的任何内容,您需要在运行时请求权限
API版本23定义了两种类型的权限,一种具有保护功能,另一种不具有保护功能。正常权限的保护仍然以旧的方式工作,例如访问网络状态和Internet,这不会造成太大危害。选中您将需要使用(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.
}
}
您需要使用(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.
}
}
请看:请看:我正在android emulator Api 23 android 6.0上运行它,如何在代码中添加权限?请看此处:有关详细信息,请查看此处的其他问题。我正在android emulator Api 23 android 6.0上运行它,如何在代码中添加权限?请看此处:有关详细信息,请查看此处的其他问题。