一些信息优先:
我第一次在Cordova创建应用程序:请参阅Cordova permissions requires android.permission.READ_CONCATS,或grantUriPermission()
现在,我使用Android Studio Builder将该应用程序创建为本地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 ResultInfo{who=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/stackoverflow中找不到任何东西。
答案介于两者之间。AndroidManifest.xml声明正确,但我的gradle构建设置将targetsdk版本更改为23。这导致新的权限系统启动(请参见@http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en)
我更改了gradle构建文件(在打开apk时检查了我的AndroidManifest之后)。现在它起作用了!