感谢 iago,我设法找到了解决方案。显然不可能使用默认的联系人应用程序来显示自定义信息,但可以创建一个联系人并显示该联系人。所以我所做的如下:
我创建了一个带有 Contact 类的小型库。这个联系人类包含我希望我的联系人拥有的所有信息。在那里,我创建了一个函数来检查提供的电子邮件地址是否存在于联系人列表中。如果是这样,此函数将启动一个显示联系人信息的意图。否则,它将启动一个意图,允许用户将联系人添加到他们的联系人列表中。完整的这个类看起来像这样:
package nl.buroboot.danielvandenberg.contact;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import java.util.ArrayList;
/**
* @author Daniël van den Berg
* @date 9/29/2015.
* <p/>
* The Contact class is a simple bridge that allows for users to add or view a contact based on entered details.
*/
public class Contact {
public final String name;
public final String email;
public final String mobile;
public final String mobile2;
public final String phone;
public final String phone2;
/**
* Creates a new contact. This contact can then be used to display or be added to the contact list.
* @param name The name of the contact.
* @param email The email of the contact.
* @param mobile The first mobile number of the contact.
* @param mobile2 The second mobile number of the contact.
* @param phone The first phone number of the contact.
* @param phone2 The second phone number of the contact.
*/
public Contact(String name,
String email, String mobile, String mobile2, String phone, String phone2) {
this.name = name;
this.email = email;
this.mobile = mobile;
this.mobile2 = mobile2;
this.phone = phone;
this.phone2 = phone2;
}
/**
* Returns a human-readable representation of this object.
* It will filter out any phone numbers that have been set to "" or null.
* This will return the following string:<br>
* <code>
* name<br>
* email<br>
* mobile<br>
* mobile2<br>
* phone<br>
* phone2
* </code>
* The line-breaks are implemented as "\n"
*/
@Override
public String toString() {
String contactString = name + "\n";
contactString += email + "\n";
if (!mobile.isEmpty() &&
!mobile.equalsIgnoreCase("null")) {
contactString += mobile + "\n";
}
if (!mobile2.isEmpty() &&
!mobile2.equalsIgnoreCase("null")) {
contactString += mobile2 + "\n";
}
if (!phone.isEmpty() &&
!phone
.equalsIgnoreCase("null")) {
contactString += phone + "\n";
}
if (!phone2.isEmpty() &&
!phone2
.equalsIgnoreCase("null")) {
contactString += phone2;
}
return contactString;
}
/**
* showOrCreateContact will show or create the contact. This function will search the phone's address book for any contact with the same e-mail address.
* If any is found, it will open that contact using the default contact app. If not, it will open an "add contact" dialog.
* @param context The context to use when firing intents.
*/
public void showOrCreateContact(Context context) {
final Cursor
cursor
= context
.getContentResolver()
.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY},
ContactsContract.CommonDataKinds.Email.DATA +
" LIKE '" +
email +
"'",
null,
null);
Log.d(
"Contact",
"Count " + cursor.getCount());
if (cursor.moveToFirst()) {
final String id = cursor.getString(0);
Log.d("Contact", "ID " + id);
Intent
intent
= new Intent(Intent.ACTION_VIEW);
String
lookupKey
= cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.LOOKUP_KEY));
long contactId = cursor.getLong(
cursor.getColumnIndexOrThrow(
ContactsContract.PhoneLookup._ID));
Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
intent.setData(uri);
context.startActivity(intent);
return;
}
Intent
intent
= new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(
ContactsContract.Intents.Insert.NAME,
name);
intent.putExtra(
ContactsContract.Intents.Insert.EMAIL,
email);
ArrayList<ContentValues> data = new ArrayList<>();
if (!mobile.isEmpty() &&
!mobile
.equalsIgnoreCase("null")) {
data.add(
addPhoneNumber(
mobile,
"Mobiel"));
}
if (!mobile2.isEmpty() &&
!mobile2
.equalsIgnoreCase("null")) {
data.add(
addPhoneNumber(
mobile2,
"Mobiel 2"));
}
if (!phone.isEmpty() &&
!phone
.equalsIgnoreCase("null")) {
data.add(
addPhoneNumber(
phone,
"Telefoon"));
}
if (!phone2.isEmpty() &&
!phone2
.equalsIgnoreCase("null")) {
data.add(
addPhoneNumber(
phone2,
"Telefoon 2"));
}
intent.putParcelableArrayListExtra(
ContactsContract.Intents.Insert.DATA,
data);
intent.putExtra(
"finishActivityOnSaveCompleted",
true);
context.startActivity(intent);
}
private ContentValues addPhoneNumber(String number, String text) {
ContentValues row = new ContentValues();
row.put(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
row.put(
ContactsContract.CommonDataKinds.Phone.NUMBER,
number);
row.put(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
row.put(
ContactsContract.CommonDataKinds.Phone.LABEL,
text);
return row;
}
}