如何保存联系人

时间:2022-12-09 17:00:30


 这两天项目有个需求,可以对事务操作人进行 【新建联系人】【保存到已有联系人】【编辑联系人(xxx)】.

 总结一下,日后备用。先看一下效果:

       

如何保存联系人

                                                                        

如何保存联系人

  一、【新建联系人】主要是给系统发一个Intent

            

public void addContact() {
Intent intent = new Intent("android.intent.action.INSERT",
People.CONTENT_URI);
if (emailAdderss != null) { // 将要增加的联系人信息放到 exta data里
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAdderss);
intent.putExtra(ContactsContract.Intents.Insert.NAME, creatorName);
}
startActivity(intent);
setResult(RESULT_FIRST_USER);
finish(); }

二、【保存到已有联系人】.(备注:选联系人中之后,到编辑该联系人,程序手动需要继续调用系统功能)

     1、先调用系统的已有联系人列表

               Intent intent = new Intent("android.intent.action.PICK",
                  People.CONTENT_URI);
     2、设置resultCode             startActivityForResult(intent,Constants.PICK_CONTACT);

     3、编写响应 resultCode 代码,做出编辑响应。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {
case Constants.PICK_CONTACT:
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
String urlStr = contactData.getEncodedPath();
String[] idStrs = urlStr.split("/");
int id = Integer.parseInt(idStrs[2]); // 主要是获得 选中人的 id 然后 进行编辑

if (Build.VERSION.SDK_INT == 4 || Build.VERSION.RELEASE.equals("1.6")) { // 发送编辑联系人请求

// for sdk1.6
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://contacts/people/" + id));
intent_.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAdderss);
startActivity(intent_); } else {
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://com.android.contacts/raw_contacts/" + id));
intent_.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAdderss); startActivity(intent_);
}
finish();
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}

三:编辑以后联系人

      1、根据email获得联系人名称

        

public  String lookupContactNameByEmail(String email) {

String where=Email.DATA1+"=?";
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ Email.CONTACT_ID}, where, new String[]{email}, null);
if(cursor.moveToNext()){
String contactWhere=Contacts._ID+"="+cursor.getInt(0);
cursor.close();
Cursor cursor2 = getContentResolver().query(Contacts.CONTENT_URI, new String[]{Contacts.DISPLAY_NAME}, contactWhere, null,null);
if(cursor2.moveToNext()){
String str = cursor2.getString(0);
return str;
}
}
return "unKnow";
}

      2、发送编辑Action

      

public void editContact() {
Integer id = null;
Cursor cursor = getContactsByEmail();
if(cursor.getCount()>0){
cursor.moveToFirst();
id = cursor.getInt(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
DebugUtils.v(TAG, emailAdderss+"===================================================+++"+id );
cursor.close();
if (Build.VERSION.SDK_INT == 4 || Build.VERSION.RELEASE.equals("1.6")) {

// for sdk1.6
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://contacts/people/" + id)); startActivity(intent_);
} else {
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://com.android.contacts/raw_contacts/" + id));
startActivity(intent_);
}

finish();
}


}