权限拒绝:在Android Studio中从ProcessRecord中打开provider com.android.providers.contacts.ContactsProvider2

我正在尝试从手机中读取联系人,并在清单文件中包含READ_CONTACTS权限时出现此错误。 奇怪的是,它在Eclipse中工作正常,但是当我将项目转换为Gradle并在Android Studio中运行时,出现此错误。

logcat说:

权限拒绝:从ProcessRecord {302f069 29282:com.GP/u0a322}(pid = 29282,uid = 10322)打开提供程序com.android.providers.contacts.ContactsProvider2需要android.permission.READ_CONTACTS或android.permission.WRITE_CONTACTS

这里是清单代码:

 <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <!-- Read Contacts from phone --> <uses-permission android:name="android.permission.read_contacts" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.READ_CALL_LOG" /> 

Solutions Collecting From Web of "权限拒绝:在Android Studio中从ProcessRecord中打开provider com.android.providers.contacts.ContactsProvider2"

由于棉花糖在运行时更新请求权限,读取联系人权限不起作用。 示例代码是

 import android.Manifest; import android.content.ContentResolver; import android.content.pm.PackageManager; import android.database.Cursor; import android.os.Build; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { // The ListView private ListView lstNames; // Request code for READ_CONTACTS. It can be any number > 0. private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find the list view this.lstNames = (ListView) findViewById(R.id.lstNames); // Read and show the contacts showContacts(); } /** * Show the contacts in the ListView. */ private void showContacts() { // Check the SDK version and whether the permission is already granted or not. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS); //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method } else { // Android version is lesser than 6.0 or the permission is already granted. List<String> contacts = getContactNames(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts); lstNames.setAdapter(adapter); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission is granted showContacts(); } else { Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show(); } } } /** * Read the name of all the contacts. * * @return a list of names. */ private List<String> getContactNames() { List<String> contacts = new ArrayList<>(); // Get the ContentResolver ContentResolver cr = getContentResolver(); // Get the Cursor of all the contacts Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // Move the cursor to first. Also check whether the cursor is empty or not. if (cursor.moveToFirst()) { // Iterate through the cursor do { // Get the contacts name String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); contacts.add(name); } while (cursor.moveToNext()); } // Close the curosor cursor.close(); return contacts; } } 

示例截图: 在这里输入图像说明 在这里输入图像说明

我从这里得到了以下源代码

我的问题是,我正在使用一个棉花糖模拟器,并已经做了一些改变,我们如何请求来自用户的权限。 因此,即使在添加权限后,它仍然要求权限。 http://developer.android.com/training/permissions/requesting.html

仅当应用程序的targetSdkVersion (在清单中指定)为23或更高时才使用新的运行时权限scheme。 所以,避免这个问题的一个方法就是让targetSdkVersion低于23.看到这里的描述:

https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

只需将其添加到<manifest>内的AndroidManifest.xml中

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