gpt4 book ai didi

android - 如何从联系人列表中获取联系人姓名、电话号码和电子邮件 ID?

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:44 25 4
gpt4 key购买 nike

我是android的新手,我需要获取我的联系人的详细信息,但详细信息只有3个

  1. 联系人姓名

  2. 联系电话和

  3. 电子邮件 ID

当我按下一个按钮时,它会显示我所有联系人的这 3 个详细信息

我正在使用 android Eclair 2.1 版。有什么解决办法吗?

最佳答案

通过下面的代码你可以做到这一点-

public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "", name = "";
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 email
cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);

int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);

int emailIdx = cursor.getColumnIndex(Email.DATA);

// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
name = cursor.getString(nameId);
Log.v(DEBUG_TAG, "Got email: " + email);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText emailEntry = (EditText) findViewById(R.id.editTextv);
EditText personEntry = (EditText) findViewById(R.id.person);
emailEntry.setText(email);
personEntry.setText(name);
if (email.length() == 0 && name.length() == 0)
{
Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
}
}
break;
}

} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}

并且,还请引用这些链接 -

  1. Get Contact Details

  2. How to Read Contacts

  3. Get All Contact Details

不要忘记添加所需的权限 -

<uses-permission android:name="android.permission.READ_CONTACTS"/>

在您的 AndroidManifest.xml 文件中。而且,只需根据您的需要修改此代码。

关于android - 如何从联系人列表中获取联系人姓名、电话号码和电子邮件 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10977887/

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