请帮我在通讯录应用程序在Android [英] Please help me in Contacts App on Android

查看:100
本文介绍了请帮我在通讯录应用程序在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用的是Android 2.0 API

Im using Android 2.1 Api

大家好

我需要添加一个字符串MIME类型到现有的联系人从电话号码除了存储用户定义的数据,电子邮件etc.Please帮助我如何添加自定义字段从我的申请。

I need to add a String MIME type into existing contacts for storing a userdefined data apart from Phone numbers,email etc.Please help me how to add that Custom Field from my application.

我用一个例子来要求,因为我是老周在Android上,请帮我。

I request with an example because I am a week old on Android please help me.

推荐答案

下面是保存一个布尔值,我的自定义MIME类型来接触一个例子。它采用最新的SDK 2.1

Here is an example that saves a boolean as my custom mime type to the contacts. It uses the latest 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆