以编程方式使用 Intent 修改或更新联系人 [英] Modify or update contacts using intent programmatically

查看:41
本文介绍了以编程方式使用 Intent 修改或更新联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 android 联系窗口中更新或修改用户联系方式.下面的代码工作正常,但是,它没有将正确的电话号码传递给 android 联系人编辑器窗口.

I would like to intent to the android contact window to update or modify a user contact details. The code below works fine, however, it does not pass the correct phone number to the android contact editor window.

这是我的代码

 private void modifyContact() {
         phonenumber="0711236763";

    mSelectedContactUri=Uri.parse(phonenumber);

    mCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


    if (mCursor != null && mCursor.moveToFirst()) {
        mLookupKeyIndex = mCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
        mCurrentLookupKey = mCursor.getString(mLookupKeyIndex);
        mIdIndex = mCursor.getColumnIndex(ContactsContract.Contacts._ID);
        mCurrentId = mCursor.getLong(mIdIndex);
    }

    mSelectedContactUri = ContactsContract.Contacts.getLookupUri(mCurrentId, mCurrentLookupKey);
    Intent editIntent = new Intent(Intent.ACTION_EDIT);

    editIntent.setDataAndType(mSelectedContactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
    editIntent.putExtra("finishActivityOnSaveCompleted", true);

    startActivity(editIntent);

    }

变量 phonenumber 是我要修改或更新的电话号码.

The variable phonenumber is the phone number for which i would like to modify or update.

推荐答案

在访问 Cursor 中的数据之前,确保它已正确初始化."它在我的代码中有效.

Make sure the Cursor is initialized correctly before accessing data from it." it works in my code.

1.在从中读取数据之前,尝试通过 moveToFirst 来定位光标.

1.Try to position cursor by moveToFirst before reading data from it.

2.check for null-> if (c != null && c.moveToFirst()) {}

2.check for null-> if (c != null && c.moveToFirst()) {}

3.check for count-> (c != null && c.getCount() >0 && c.moveToFirst()){}

3.check for count-> (c != null && c.getCount() >0 && c.moveToFirst()){}

这篇关于以编程方式使用 Intent 修改或更新联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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