android - 如何在 Android 上使用联系人应用程序?

标签 android

我正在使用 Android 2.1 API

我需要将字符串 MIME 类型添加到现有联系人中,以存储除电话号码、电子邮件等之外的用户定义数据。请帮助我如何从我的应用程序中添加该自定义字段。

我请求一个示例,因为我已经使用 Android 一周了。

最佳答案

这是一个将 bool 值作为我的自定义 MIME 类型保存到联系人的示例。它使用最新的SDK 2.1

public static final String MIMETYPE_FORMALITY = "vnd.android.cursor.item/useformality";
public clsMyClass saveFormality() {
        try {
            ContentValues values = new ContentValues();
            values.put(Data.DATA1, this.getFormality() ? "1" : "0");
            int mod = ctx.getContentResolver().update(
                    Data.CONTENT_URI,
                    values,
                    Data.CONTACT_ID + "=" + this.getId() + " AND "
                            + Data.MIMETYPE + "= '"
                            + clsContacts.FORMALITY_MIMETYPE + "'", null);

            if (mod == 0) {
                values.put(Data.CONTACT_ID, this.getId());
                values.put(Data.MIMETYPE, clsContacts.FORMALITY_MIMETYPE);
                ctx.getContentResolver().insert(Data.CONTENT_URI, values);
            }
        } catch (Exception e) {
            Log.v(TAG(), "saveFormality failed");
        }
     return this;
    }

public boolean getFormality() {
     if (data.containsKey(FORMALITY)) {
        return data.getAsBoolean(FORMALITY);
    } else {
        // read formality
        Cursor c = readDataWithMimeType(clsContacts.MIMETYPE_FORMALITY, this.getId());
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    this.setFormality(c.getInt(0) == 1);
                    return (c.getInt(0) == 1);
                }
            } finally {
                c.close();
            }
        }
        return false;
    }

}
public clsMyClass setFormality(Boolean value) {
    data.remove(FORMALITY);
    data.put(FORMALITY, value);
    return this;
}

/**
 * Utility method to read data with mime type
 *
 * @param mimetype String representation of the mimetype used for this type
 *            of data
 * @param contactid String representation of the contact id
 * @return
 */
private Cursor readDataWithMimeType(String mimetype, String contactid) {
    return ctx.getContentResolver().query(
            Data.CONTENT_URI,
            new String[] {
                Data.DATA1
            },
            Data.RAW_CONTACT_ID + "=" + contactid + " AND " + Data.MIMETYPE + "= '" + mimetype
                    + "'", null, null);
}

用法是

objContact.setFormality(true).saveFormality();

关于android - 如何在 Android 上使用联系人应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3429512/

相关文章:

android - "fatal error: Eigen/Dense: No such file or directory"//特征库

android - 您如何即时创建应用程序?

android - 透明背景帧动画

Android startBluetoothSco 不启动 sco 但 isBluetoothScoOn 返回 true

java - Android 如何加载 ListView 标题的新 fragment onClick

android - 如何将本地 SQLite 数据库与 jQuery Mobile 集成

android - Gradle构建失败(不兼容的魔术值)

android - 如何在 Android Spinner 中隐藏一项

android - 尝试实现多语言支持时应用程序崩溃

android - 在后续的挂起方法调用之间发出 LiveData 以更新 UI