gpt4 book ai didi

java - 即使声明了 read_contacts 权限后也会出现权限错误

转载 作者:行者123 更新时间:2023-12-01 21:59:52 25 4
gpt4 key购买 nike

所以我最初只想访问联系人姓名,并且我只在 list 中声明了我的许可。当时我的代码运行良好。但现在我也尝试访问联系号码,这在 logcat 中给出了以下内容:

    2019-11-05 18:56:02.738 30808-30919/? E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5361, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:705)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:565)
at android.content.ContentProvider$Transport.query(ContentProvider.java:224)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:752)
2019-11-05 18:54:09.212 5113-5113/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fresh, PID: 5113
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2644i1d5681210d9c25a6.1450iSkype_108/738 flg=0x1 }} to activity {com.example.android.fresh/com.example.android.fresh.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5113, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.app.ActivityThread.deliverResults(ActivityThread.java:4506)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5113, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:432)
at android.content.ContentResolver.query(ContentResolver.java:847)
at android.content.ContentResolver.query(ContentResolver.java:762)
at android.content.ContentResolver.query(ContentResolver.java:720)
at com.example.android.fresh.MainActivity.getContactNumber(MainActivity.java:163)
at com.example.android.fresh.MainActivity.onActivityResult(MainActivity.java:129)
at android.app.Activity.dispatchActivityResult(Activity.java:7476)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4499)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548) 
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6898) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2019-11-05 18:56:02.740 5361-5361/com.example.android.fresh E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fresh, PID: 5361
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2644i179d0ece8ca0b7f9.3789r1113-2916221214.1450iSkype_1113.1419r4429-2916221214.847r4510-2916221214/4513 flg=0x1 }} to activity {com.example.android.fresh/com.example.android.fresh.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5361, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.app.ActivityThread.deliverResults(ActivityThread.java:4506)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5361, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:432)
at android.content.ContentResolver.query(ContentResolver.java:847)
at android.content.ContentResolver.query(ContentResolver.java:762)
at android.content.ContentResolver.query(ContentResolver.java:720)
at com.example.android.fresh.MainActivity.getContactNumber(MainActivity.java:163)
at com.example.android.fresh.MainActivity.onActivityResult(MainActivity.java:129)
at android.app.Activity.dispatchActivityResult(Activity.java:7476)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4499)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548) 
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6898) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

这是 getContactName() 中的相关代码

    Cursor cur = getContentResolver().query(uriContact, null, null, null, null);

if (cur.moveToFirst()) {

contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));}

以及getContactNumber()中的相关代码

   Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);

if (cursorID.moveToFirst()) {

contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}

cursorID.close();

Log.d(TAG, "Contact ID: " + contactID);

// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

new String[]{contactID},
null);

if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
returnval = true;
}

cursorPhone.close();

如何解决这个问题?

最佳答案

用这个


public class MainActivity extends AppCompatActivity {

// 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);

// 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.
getContactNames();
}
}

@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 void getContactNames() {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};

Cursor people = getContentResolver().query(uri, projection, null, null, null);

int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

if(people.moveToFirst()) {
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// add number to list
// Do work...
} while (people.moveToNext());
}
people.close();
}
}

关于java - 即使声明了 read_contacts 权限后也会出现权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58712648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com