如何在android中读取联系人
我正在开发一个安卓手机短信应用程序,在这个应用程序中,我想使用以下代码从联系人中读取联系人号码如何在android中读取联系人,android,Android,我正在开发一个安卓手机短信应用程序,在这个应用程序中,我想使用以下代码从联系人中读取联系人号码 Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); ContactPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
ContactPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);
它在模拟器上运行良好,但在设备上运行不正常。我应该进行哪些更改以使其在设备上运行
这是我的onactivityresult函数
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor=null;
try
{
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
cursor.moveToFirst();
int phoneIdx = cursor.getColumnIndex(Phone.NUMBER);
if (cursor.moveToFirst())
{
phonenofromcontact = cursor.getString(phoneIdx);
finallistofnumberstosendmsg +=","+phonenofromcontact;
Log.v(DEBUG_TAG, "Got phone no : " + phonenofromcontact);
}
else
{
Log.w(DEBUG_TAG, "No results");
}
}
catch(Exception e)
{
Log.e(DEBUG_TAG, "Failed to get contact number", e);
}
finally
{
if (cursor != null)
{
cursor.close();
}
}
phonePhoneno= (EditText)findViewById(R.id.phonenofromcontact);
phonePhoneno.setText(finallistofnumberstosendmsg);
//phonePhoneno.setText(phonenofromcontact);
if(phonenofromcontact.length()==0)
{
Toast.makeText(this, "No contact number found for this contact",
Toast.LENGTH_LONG).show();
}
break;
}
}
else
{
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
您需要将权限
android.permission.READ\u CONTACTS
添加到您的AndroidManifest.xml中。需要android清单文件中的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" >
</uses-permission>
您正在使用以下方法过滤您的意图:
ContactPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
它告诉联系人提供商仅返回包含电话内容类型的联系人信息
因此,联系人提供商返回的ID是联系人合同数据表的联系人ID
,而不是联系人ID
尝试将光标查询更改为:
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone._ID + "=?",
new String[]{id}, null);
这将返回您选择的联系人号码
顺便说一句,如果您没有调用Intent上的contactpickerinent.setType
(如果您进行了上述更改,那么您仍然需要调用它!)
它可能是幸运地在你的模拟器上工作的-我猜也许你有一个联系人,在你的模拟器上有一个电话号码?数据表的CONTACT\u ID
和\u ID
很可能都是1,这将恰好返回正确的行。只需更改即可
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
到
它以什么方式不起作用?它会崩溃吗?还是不回复联系人?请提供更多信息!它没有返回联系人号码。我是否也应该提供onActivityResult函数的代码以使其更清晰?您是否也可以在此处显示onActivityResult函数。是否要选择特定联系人或阅读联系人簿中的所有联系人?@KernowBunney问题已更新。我已获得该权限。正如我所提到的,它正在emulator上工作。@kaidul islam sazal它正在工作,但我们的要求有所不同。.我想读取联系人号码并添加到我的应用程序中的edittext。不工作的消息“找不到此联系人的联系人号码”表示phonenofromcontact.length()==0,因为它写在代码中。你确定吗?当我将您的代码粘贴到活动中时,该修复对我有效。您尝试了什么:将联系人ID更改为_ID,或删除setType?另外,不要两者都做!!
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone._ID + "=?",
new String[]{id}, null);