這兩天項目有個需求,可以對事務操作人進行 【新建聯系人】【保存到已有聯系人】【編輯聯系人(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、設置resultCodestartActivityForResult(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();
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
