标签:
该方法用于从ContentProvider中获取数据。
//代码
ContentResolver cr = getContentResolver(); //向联系人插入数据 ContentValues cValue = new ContentValues(); Uri uri = cr.insert(ContactsContract.RawContacts.CONTENT_URI, cValue); Long raw_contact_id = ContentUris.parseId(uri); cValue.clear(); //插入联系人 cValue.put(StructuredName.RAW_CONTACT_ID, raw_contact_id); cValue.put(StructuredName.DISPLAY_NAME, "Breath"); cValue.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); uri = cr.insert(ContactsContract.Data.CONTENT_URI, cValue); cValue.clear(); //插入联系电话 cValue.put(Phone.RAW_CONTACT_ID, raw_contact_id); cValue.put(Phone.NUMBER, "1389746423"); cValue.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE); uri = cr.insert(ContactsContract.Data.CONTENT_URI, cValue); Log.i("Info", "插入数据");//代码
ContentResolver cr = getContentResolver(); Cursor c = cr.query(Contacts.CONTENT_URI, new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); if (c != null){ while(c.moveToNext()){ Log.i("info", "username : " + c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME))); int id = c.getInt(c.getColumnIndex(Contacts._ID)); //根据联系人ID查询出联系人的电话号码 Cursor cc = cr.query(Phone.CONTENT_URI, new String[]{Phone.NUMBER, Phone.TYPE}, Phone.CONTACT_ID + "=" + id, null, null); if(cc != null){ while(cc.moveToNext()){ int type = cc.getInt(cc.getColumnIndex(Phone.TYPE)); String phone = cc.getString(cc.getColumnIndex(Phone.NUMBER)); switch (type){ case Phone.TYPE_HOME: Log.i("info", "Phone_Home :" + phone); break; case Phone.TYPE_MOBILE: Log.i("info", "Phone_Mobile :" + phone); break; } } cc.close(); } //根据联系人ID查询出联系人的邮箱地址 Cursor ccc = cr.query(Email.CONTENT_URI, new String[]{Email.DATA,Email.TYPE}, Email._ID + "=" + id, null, null); if(ccc != null){ while (ccc.moveToNext()){ int type = ccc.getInt(ccc.getColumnIndex(Email.TYPE)); String email = ccc.getString(ccc.getColumnIndex(Email.DATA)); switch (type){ case Email.TYPE_HOME: Log.i("info", "Email_Home : " + email); break; case Email.TYPE_WORK: Log.i("info", "Email_Work : " + email); break; } } ccc.close(); } } c.close(); }
标签:
原文地址:http://my.oschina.net/u/1468096/blog/505113