PickContact 需要 android.permission.READ_CONTACTS 或 grantUriPermission()
Posted
技术标签:
【中文标题】PickContact 需要 android.permission.READ_CONTACTS 或 grantUriPermission()【英文标题】:PickContact requires android.permission.READ_CONTACTS, or grantUriPermission() 【发布时间】:2016-02-05 17:15:36 【问题描述】:先了解一些信息:
我首先在 Cordova 中创建了该应用程序:请参阅 Cordova permissions requires android.permission.READ_CONTACTS, or grantUriPermission()
现在我使用 Android Studio Builder 将应用程序创建为原生 Android 应用程序,它有同样的错误。
我使用的是 Android 6.0 (SDK 23)
根据文档,ContactPick 不需要权限:http://developer.android.com/guide/components/intents-common.html#Contacts
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.
(但我还是声明了)
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="me.sapico.notify_arrival">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
<uses-permission-sdk-23 android:name="android.permission.READ_CONTACTS" />
<uses-permission-sdk-23 android:name="android.permission.LOCATION_HARDWARE" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的代码(它使用 ListView 的点击)
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null)
startActivityForResult(intent, REQUEST_SELECT_CONTACT);
);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK)
String phone = getNumberContacts(data);
String t = "";
/* Uri contactUri = data.getData();
String[] projection = ;//ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst())
int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
//...
*/
这是堆栈跟踪
11-05 01:47:17.008 26632-26688/me.sapico.notify_arrival E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaefbd3e0
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: FATAL EXCEPTION: main
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: Process: me.sapico.notify_arrival, PID: 26632
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1, result=-1, data=Intent dat=content://com.android.contacts/contacts/lookup/258i49688c038aae982f.3789r2248-292F313F3943312F31533F4545/897 flg=0x1 to activity me.sapico.notify_arrival/me.sapico.notify_arrival.MainActivity: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=26632, uid=10149 requires android.permission.READ_CONTACTS, or grantUriPermission()
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.-wrap16(ActivityThread.java)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=26632, uid=10149 requires android.permission.READ_CONTACTS, or grantUriPermission()
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1599)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:491)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:434)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at me.sapico.notify_arrival.MainActivity.getNumberContacts(MainActivity.java:200)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at me.sapico.notify_arrival.MainActivity.onActivityResult(MainActivity.java:175)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:6428)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.-wrap16(ActivityThread.java)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我在权限/请求电话信息方面有很多变体。当我尝试获取号码时,它通常会失败..(我在获取用户名时成功了一次)。
有什么想法吗?它也在我的 Cordova 应用程序中(我以前以为是 Cordova),但现在它也在原生 Android 中。我知道 Android 6.0 最近是新的,但我在 docs/*** 中找不到任何内容。
【问题讨论】:
您的问题出在getNumberContacts()
,不在您问题中显示的源代码中。
不,可能是因为inthecheesefactory.com/blog/…,明天我会调查的:)
它在失败交付结果 ResultInfo 时崩溃,所以由于某种原因它不能给我联系信息
【参考方案1】:
答案介于两者之间。 AndroidManifest.xml 已正确声明,但我的 gradle 构建设置将 targetsdk 版本更改为 23。这导致新的权限系统启动(参见 @http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en)
我更改了 gradle 构建文件(在解压 apk 时检查了我的 AndroidManifest)。现在可以使用了!
【讨论】:
这对我帮助很大。 你找了多久了? :p 只是预感花了一段时间:p以上是关于PickContact 需要 android.permission.READ_CONTACTS 或 grantUriPermission()的主要内容,如果未能解决你的问题,请参考以下文章