[android] 儲存連絡人到系統通訊錄

來源:互聯網
上載者:User

標籤:

對應著讀連絡人,把資料寫進去,市場上的社交類應用經常會有這樣的功能

  1. 向raw_contacts表中添加一個id
  2. 向data表裡面添加對應的資料

擷取ContentResolver對象,通過getContentResolver()方法

調用ContentResolver對象的query()方法,查詢raw_contacts表,得到Cursor對象

調用Cursor對象的moveToLast()方法,遊標移動到最後一行

擷取到最後一行記錄的contact_id,新的id就是它加上1

調用ContentResolver對象的insert(uri,values)方法,參數:values是ContentValues對象把新的id put()進去

 

擷取到ContentResolver對象後調用insert(uri,values)方法,參數:Uri應該是data表的Uri規則,ContentValues對象的,put(key,value),”raw_contact_id”=>上面的id,”mimetype”=>”vnd.android.cursor.item/phone_v2”,”data1”=>”電話號碼”

 

然後添加姓名,和上面一樣只不過在ContentValues對象裡面,

Put “mimietype”=>”vnd.android.cursor.item/name”索引值對

 

    public void writeContacts(View v){        ContentResolver resolver=getContentResolver();        Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");        Uri dataUri=Uri.parse("content://com.android.contacts/data");        //查出最後一個id        Cursor cursor=resolver.query(uri, new String[]{"_id"}, null, null, null);        cursor.moveToLast();        int lastId=cursor.getInt(0);        int newId=lastId+1;        //插入一個連絡人id        ContentValues values=new ContentValues();        values.put("contact_id", newId);        resolver.insert(uri, values);        //插入電話資料        ContentValues dataValues=new ContentValues();        dataValues.put("raw_contact_id", newId);        dataValues.put("mimetype", "vnd.android.cursor.item/phone_v2");        dataValues.put("data1", "110");        resolver.insert(dataUri, dataValues);        //插入姓名資料        ContentValues data1Values=new ContentValues();        data1Values.put("raw_contact_id", newId);        data1Values.put("mimetype", "vnd.android.cursor.item/name");        data1Values.put("data1", "police1");        resolver.insert(dataUri, data1Values);                Toast.makeText(this, "寫連絡人成功", 0).show();    }

 

[android] 儲存連絡人到系統通訊錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.