我正在尝试使用代码来选择联系人并检索电话号码。 我找到了一段在Emulator(android版本2.3.3)和我兄弟的手机(Jelly bean)上工作得很好的代码,但不是我自己的设备(android 2.3.4)
这是代码: 转到联系人
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
从联系人回来时:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
//int type = c.getInt(1);
showSelectedNumber(0, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(int type, String number) {
Toast.makeText(this,number, Toast.LENGTH_LONG).show();
}
同样,这适用于模拟器(android 2.3.4)和Jelly bean,但不适用于我的设备(android 2.3.5)
这是日志中的内容:
10-10 22:07:15.775: E/AndroidRuntime(5075): FATAL EXCEPTION: main
10-10 22:07:15.775: E/AndroidRuntime(5075): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/2848 (has extras) }} to activity {com.example.omer/com.example.omer.SmsInfo}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/2848 from pid=5075, uid=10125 requires android.permission.READ_CONTACTS
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.deliverResults(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.handleSendResult(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.access$2000(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.os.Handler.dispatchMessage(Handler.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.os.Looper.loop(Looper.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.main(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 22:07:15.775: E/AndroidRuntime(5075): at java.lang.reflect.Method.invoke(Method.java:507)
10-10 22:07:15.775: E/AndroidRuntime(5075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at dalvik.system.NativeStart.main(Native Method)
10-10 22:07:15.775: E/AndroidRuntime(5075): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/2848 from pid=5075, uid=10125 requires android.permission.READ_CONTACTS
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.os.Parcel.readException(Parcel.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.content.ContentProviderProxy.query(ContentProviderNative.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.content.ContentResolver.query(ContentResolver.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at com.example.omer.SmsInfo.onActivityResult(SmsInfo.java:87)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.Activity.dispatchActivityResult(Activity.java)
10-10 22:07:15.775:E / AndroidRuntime(5075):... 12更多
答案 0 :(得分:1)
好的..刚刚搞定了 我必须向清单文件添加权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
答案 1 :(得分:0)
根据错误消息:
java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 1,result = -1,data = Intent { dat = content://com.android.contacts/data/2848(有额外内容)}}到 activity {com.example.omer / com.example.omer.SmsInfo}: java.lang.SecurityException:Permission Denial:reading com.android.providers.contacts.HtcContactsProvider2 uri 内容://com.android.contacts/data/2848来自pid = 5075,uid = 10125 需要android.permission.READ_CONTACTS
您需要添加权限android.permission.READ_CONTACTS