亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能

 更新時(shí)間:2017年05月05日 11:09:27   作者:夕陽(yáng)部落  
這篇文章主要介紹了Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能,需要的朋友可以參考下

最近在做手機(jī)聯(lián)系人的功能模塊的時(shí)候,遇到了很多的坑,在網(wǎng)上搜索的有一些所謂的最全的手機(jī)聯(lián)系人開發(fā)的介紹還存在一些bug,所以我把我最近的項(xiàng)目心得和方法寫下來(lái),既能幫助大家減少了解android開發(fā)手機(jī)聯(lián)系人的門檻,好,廢話少說(shuō),接下來(lái)直奔主題。

一、深入淺出手機(jī)聯(lián)系人的前奏(小米手機(jī)的data表跟模擬器的data表不一樣)

1、手機(jī)聯(lián)系人主要是對(duì)contacts2.db數(shù)據(jù)庫(kù)表的操縱,這個(gè)數(shù)據(jù)庫(kù)中有三個(gè)表是比較重要的,分別是data,raw_contacts,mimetyps這三個(gè)表。在下面的增刪查改模塊中,主要是根據(jù)表之間的關(guān)系來(lái)關(guān)聯(lián)的處理的。

1.1、data表

 

1.2、mimetypes表

這里寫圖片描述 

1.3、raw_contacts表

這里寫圖片描述 

1.4、下載contacts2.db表的方法

在eclipse中打開android模擬器,在eclipse導(dǎo)航欄的window->show View->other..->File Explorer.單擊eclipse導(dǎo)航欄右上角的DDMS,F(xiàn)ile Explorer 下的data->data->com.android.providers.contact->databases下就有contacts2.db數(shù)據(jù)庫(kù)了。單擊然后保存就可以了。

查看SqlLite軟件的下載鏈接

這里寫圖片描述 

2、深入淺出之手機(jī)聯(lián)系人查詢模塊

 /**
 * 通過(guò)輸入獲取電話號(hào)碼
 */
 public void number(String name1,long rawContactId) {
 //使用ContentResolver查找聯(lián)系人數(shù)據(jù)
 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
 //遍歷查詢結(jié)果,找到所需號(hào)碼
 while (cursor.moveToNext()) {
  //獲取聯(lián)系人ID
  String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
  //獲取聯(lián)系人的名字
  String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  if (name1.equals(contactName)) {
  //使用ContentResolver查找聯(lián)系人的電話號(hào)碼和用戶名
  Cursor phone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
  if (phone.moveToNext()) {  
  String phoneNumber1 = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));//電話號(hào)碼
  String phoneName1 = phone.getString(phone.getColumnIndex(Phone.DISPLAY_NAME));//姓名 
  phoneNumber.setText(phoneNumber1);
  name.setText(phoneName1);
  }
  //獲取郵箱信息
  Cursor emails =getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+contactId,null,null);
   while(emails.moveToNext()){
   String emailAddress =emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
   email.setText(emailAddress);
   }
   //獲取IM信息
   Cursor cursorQQ =getContentResolver().query(ContactsContract.Data.CONTENT_URI,null,ContactsContract.Data.RAW_CONTACT_ID+"="+rawContactId + " AND " + "mimetype_id=2",null,null);
   while(cursorQQ.moveToNext()){
   //獲取公司信息
   String im1 = cursorQQ.getString(cursorQQ.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)); 
   qq.setText(im1);
  }
   //獲取公司信息
   Cursor cursorCompany =getContentResolver().query(ContactsContract.Data.CONTENT_URI,null,ContactsContract.Data.RAW_CONTACT_ID+"="+rawContactId + " AND " + "mimetype_id = 4",null,null); 
   while(cursorCompany.moveToNext()){
   String company1=cursorCompany.getString(cursorCompany.getColumnIndex("data1"));
   String position1=cursorCompany.getString(cursorCompany.getColumnIndex("data4"));
   position.setText(position1); 
   company.setText(company1);
   }
  } 
 }
}

3、深入淺出之手機(jī)聯(lián)系人增加模塊 

 //增加聯(lián)系人
 public void addContact(String name, String phoneNumber,String email,String company,String position,String im) { 
 /* 往 raw_contacts 中添加數(shù)據(jù),并獲取添加的id號(hào)*/ 
 /* 往 raw_contacts 中添加數(shù)據(jù),并獲取添加的id號(hào)*/ 
 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); 
 ContentValues values = new ContentValues(); 
 long rawContactId= ContentUris.parseId(resolver.insert(uri, values)); 
 //插入data表 
 uri = Uri.parse("content://com.android.contacts/data"); 
 // 向data表插入數(shù)據(jù) 
 if (name != "") { 
  values.put("raw_contact_id", rawContactId); 
  values.put("mimetype", "vnd.android.cursor.item/name"); 
  values.put("data2", name); 
  resolver.insert(uri, values); 
 } 
 // 向data表插入電話號(hào)碼 
 if ( phoneNumber != "") { 
  values.clear(); 
  values.put("raw_contact_id", rawContactId); 
  values.put("mimetype", "vnd.android.cursor.item/phone_v2"); 
  values.put("data2", "2"); 
  values.put("data1", phoneNumber); 
  resolver.insert(uri, values);
 } 
 //向data表中插入郵箱
 if (email!= "") { 
  // 添加Email 
  values.clear(); 
  values.put("raw_contact_id", rawContactId);
  values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); 
  values.put(Email.DATA, email);
  values.put(Email.TYPE, Email.TYPE_WORK); 
  resolver.insert(uri, values); 
 }
 //向data表中插入聯(lián)系人的組織
 if(company!=""&&position!=""){
  //organization
  values.clear(); 
  values.put(Data.RAW_CONTACT_ID, rawContactId); 
  values.put(Organization.MIMETYPE, Organization.CONTENT_ITEM_TYPE); 
  values.put(Organization.LABEL, name); 
  values.put(Organization.TITLE, position); 
  values.put(Organization.COMPANY, company); 
  values.put(Organization.TYPE,Organization.TYPE_WORK); 
  context.getContentResolver().insert(uri, values); 
 }
 //向data表中插入聯(lián)系人的QQ
 if(im!=""){
  //im
  values.clear(); 
  values.put("raw_contact_id", rawContactId); 
  values.put("mimetype", "vnd.android.cursor.item/im"); 
  values.put(Im.DATA, im); 
  values.put(Im.TYPE, Im.TYPE_WORK); 
  resolver.insert(uri, values); 
 } 
 } 

4、深入淺出之手機(jī)聯(lián)系人更新模塊

// 更新聯(lián)系人 
 public void updataContact(long rawContactId,String name,String number,String email,String company,String position,String im) { 
  Uri uri = Uri.parse("content://com.android.contacts/data");//對(duì)data表的所有數(shù)據(jù)操作
  ContentValues values = new ContentValues();
  //更新電話號(hào)碼
  values.put("data1", number);
  resolver.update(uri, values, "mimetype_id=? and raw_contact_id=?", new String[]{"5", rawContactId+""}) ;
  //更新聯(lián)系人姓名
  values.clear();
  values.put("data1", name);
  resolver.update(uri, values, "mimetype_id=? and raw_contact_id=?", new String[]{"7", rawContactId+""}) ;
  //更新email
  values.clear();
  values.put("data1", email);
  resolver.update(uri, values, "mimetype_id=? and raw_contact_id=?", new String[]{"1", rawContactId+""}) ; 
  //更新im
  values.clear();
  values.put("data1", im);
  resolver.update(uri, values, "mimetype_id=? and raw_contact_id=?", new String[]{"2", rawContactId+""}) ; 
  //更新company
  values.clear();
  values.put("data1", company);
  values.put("data3",name);
  values.put("data4",position);
  resolver.update(uri, values, "mimetype_id=? and raw_contact_id=?", new String[]{"4", rawContactId+""}) ; 
 } 

5、深入淺出之手機(jī)聯(lián)系人刪除模塊

// 刪除聯(lián)系人 
 public void deleteContact(long rawContactId) { 
 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
  Cursor cursor =
  resolver.query(uri,new String[]{RawContacts._ID},"contact_id=?",new String[]{String.valueOf(rawContactId) }, null ); 
  if(cursor.moveToFirst()){
  int id = cursor.getInt(0);
  resolver.delete(uri, "_id=?",new String[]{id+""});
  uri = Uri.parse("content://com.android.contacts/data");
  resolver.delete(uri, "raw_contact_id=?",new String[]{id+""});
  cursor.close();
  } 
 } 

6、大話自制手機(jī)聯(lián)系人軟件與系統(tǒng)手機(jī)聯(lián)系人軟件的區(qū)別

6.1、系統(tǒng)的手機(jī)聯(lián)系人軟件在刪除單個(gè)聯(lián)系人的時(shí)候不是直接刪除的,然是通過(guò)把某個(gè)值設(shè)置為0,使得該手機(jī)聯(lián)系人信息不可見(jiàn),根據(jù)網(wǎng)上的大多數(shù)實(shí)例都是直接把data數(shù)據(jù)庫(kù)表中的單個(gè)聯(lián)系人的信息直接delete了。

相關(guān)文章

最新評(píng)論