通讯录数据的存取(三)—— 使用Intent修改通讯录

使用Intent修改通讯录

使用Intent增加联系人

该操作会调用通讯录应用程序将新的联系人信息插入到Contacts Provider的ContactsContract.RawContacts 表中。如果有必要的话,通讯录应用程序会在创建联系人时,弹框提醒用户输入账户类型和要使用的账号。如果联系人已经存在的话,会提醒用户选择取消插入信息,这种情况下不会再创建联系人。

构造Intent

使用Intents.Insert.ACTION作为行为来创建Intent对象, 并设置MIME类型为RawContacts.CONTENT_TYPE. 代码如下:

// Creates a new Intent to insert a contact 
Intent intent = new Intent (Intents. Insert.ACTION);
// Sets the MIME type to match the Contacts Provider 
intent.setType( ContactsContract.RawContacts .CONTENT_TYPE );

另外,可以将电话号码或email地址作为扩展的数据插入到上述intent对象中。该扩展数据的键值需要使用Intents.Insert中的相应的常量。通讯录应用程序会在创建联系人的界面显示上述数据,并且允许用户做进一步的修改。

/* Assumes EditText fields in your UI contain an email address
 * and a phone number.
 *
 */
private EditText mEmailAddress = (EditText ) findViewById (R .id .email );
private EditText mPhoneNumber = (EditText ) findViewById (R .id .phone );
...
/*
 * Inserts new data into the Intent. This data is passed to the
 * contacts app's Insert screen
 */
// Inserts an email address 
intent.putExtra( Intents.Insert .EMAIL , mEmailAddress .getText ())
/*
 * In this example, sets the email type to be a work email.
 * You can set other email types as necessary.
 */
      .putExtra( Intents.Insert .EMAIL_TYPE , CommonDataKinds.Email .TYPE_WORK )
// Inserts a phone number
      .putExtra( Intents.Insert .PHONE , mPhoneNumber .getText ())
/*
 * In this example, sets the phone type to be a work phone.
 * You can set other phone types as necessary.
 */
      .putExtra( Intents.Insert .PHONE_TYPE , Phone.TYPE_WORK);
    /* Sends the Intent
     */
    startActivity( intent);
使用Intent编辑已经存在的联系人

创建该Intent需要添加联系人的Contacts.CONTENT_LOOKUP_URI和MIME类型Contacts.CONTENT_ITEM_TYPE。可以使用Intent的扩充数据来添加联系人的其它信息。需要注意的是有些与姓名相关的列是不能使用intent来修改的,具体请参见ContactsContract.Contacts的“Update”项。

通讯录的Lookup值

每个联系人的LOOKUP_KEY值是获取该联系人的唯一标识。即使provider修改来该联系人的行ID去处理一些内部的操作,它仍然保持不变。

构建Intent

使用ACTION_EDIT作为行为来创建Intent对象,调用setDataAndType()设置联系人的Contacts.CONTENT_LOOKUP_URI和MIME类型Contacts.CONTENT_ITEM_TYPE。因为调用setType()会覆盖Intent已有的数据,因此必须同时设置数据(即上述URI)和MIME类型。代码如下:

   // The Cursor that contains the Contact row 
    public Cursor mCursor; 
    // The index of the lookup key column in the cursor 
    public int mLookupKeyIndex;
    // The index of the contact's _ID value 
    public int mIdIndex;
    // The lookup key from the Cursor 
    public String mCurrentLookupKey;
    // The _ID value from the Cursor 
    public long mCurrentId;
    // A content URI pointing to the contact 
    Uri mSelectedContactUri; 
    ...
    /*
     * Once the user has selected a contact to edit,
     * this gets the contact's lookup key and _ID values from the
     * cursor and creates the necessary URI.
     */
    // Gets the lookup key column index 
    mLookupKeyIndex = mCursor. getColumnIndex(Contacts .LOOKUP_KEY );
    // Gets the lookup key value 
    mCurrentLookupKey = mCursor. getString(mLookupKeyIndex );
    // Gets the _ID column index 
    mIdIndex = mCursor. getColumnIndex(Contacts ._ID );
    mCurrentId = mCursor. getLong( mIdIndex); 
    mSelectedContactUri = 
            Contacts .getLookupUri (mCurrentId , mCurrentLookupKey );
    ...
    // Creates a new Intent to edit a contact 
    Intent editIntent = new Intent (Intent. ACTION_EDIT);
    /*
     * Sets the contact URI to edit, and the data type that the
     * Intent must match
     */
    editIntent.setDataAndType( mSelectedContactUri,Contacts .CONTENT_ITEM_TYPE );
添加导航标志

在Android4.0(API 14)及以后,通讯录的一个问题导致错误的导航:即某应用程序发生对通讯录的编辑intent,用户编辑并保存信息后,当用户点击后退按钮时,退回到通讯录列表而不是该应用程序。
为解决上述问题,在Android4.0.3(API 15)及以后,需要在上述intent中添加扩展的数据键值:finishActivityOnSaveCompleted,并设置它的值为true.如下:

// Sets the special extended data for navigation 
    editIntent.putExtra( "finishActivityOnSaveCompleted" , true);
添加其他数据

调用putExtra()来添加联系人的其它信息。该扩展数据的键值需要使用Intents.Insert中的相应的常量。需要注意的是有些与姓名相关的列是不能使用intent来修改的,具体请参见ContactsContract.Contacts的“Update”项。

让用户选择添加或创建联系人

使用ACTION_INSERT_OR_EDIT作为行为来创建Intent对象,设置MIME类型为Contacts.CONTENT_ITEM_TYPE,但不设置URI。
当发生上述intent后,通讯录应用程序会显示联系人列表。用户可以选择插入新的联系人或选择已经存在的联系人做修改。代码如下:

  // Creates a new Intent to insert or edit a contact 
    Intent intentInsertEdit = new Intent (Intent. ACTION_INSERT_OR_EDIT);
    // Sets the MIME type 
    intentInsertEdit. setType( Contacts.CONTENT_ITEM_TYPE );
    // Add code here to insert extended data, if desired 
    ...
    // Sends the Intent with an request ID 
    startActivity( intentInsertEdit);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值