博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Android2.2的联系人的基本操作
阅读量:5153 次
发布时间:2019-06-13

本文共 14296 字,大约阅读时间需要 47 分钟。

Activty部分:
TestContacts.java:
  1. package waga.android.study.TestContacts;
  2. import android.app.Activity;
  3. import android.content.ContentResolver;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.os.Bundle;
  7. import android.provider.ContactsContract;
  8. import android.provider.ContactsContract.RawContacts;
  9. import android.view.Menu;
  10. import android.view.MenuInflater;
  11. import android.view.MenuItem;
  12. import android.widget.TextView;
  13. public class TestContacts extends Activity
  14. {
  15. TextView myTextView;
  16. String str;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. setTitle(R.string.ReadContact);
  23. }
  24. // 创建选项菜单
  25. @Override
  26. public boolean onCreateOptionsMenu(Menu menu)
  27. {
  28. super.onCreateOptionsMenu(menu);
  29. MenuInflater inflater = getMenuInflater();
  30. inflater.inflate(R.layout.menu, menu);
  31. return true;
  32. }
  33. // 响应选项菜单
  34. @Override
  35. public boolean onOptionsItemSelected(MenuItem item)
  36. {
  37. switch (item.getItemId())
  38. {
  39. // 新建联系人
  40. case R.id.menu_add:
  41. {
  42. // new一 个Intent对 象 , 并指定class
  43. Intent intent = new Intent();
  44. intent.setClass(this, NewContactActivity.class);
  45. startActivity(intent);
  46. return true;
  47. }
  48. // 删除联系人
  49. case R.id.menu_delete:
  50. {
  51. // new一 个Intent对 象 , 并指定class
  52. Intent intent = new Intent();
  53. intent.setClass(this, DeleteContactActivity.class);
  54. startActivity(intent);
  55. return true;
  56. }
  57. // 更新联系人
  58. case R.id.menu_update:
  59. {
  60. // new一 个Intent对 象 , 并指定class
  61. Intent intent = new Intent();
  62. intent.setClass(this, UpdateContactActivity.class);
  63. startActivity(intent);
  64. return true;
  65. }
  66. // 退出
  67. case R.id.menu_exit:
  68. {
  69. finish();
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. @Override
  76. protected void onResume()
  77. {
  78. super.onResume();
  79. TextView myTextView = (TextView)findViewById(R.id.ContactsInfo);
  80. str = "";
  81. // 读取所有的联系人
  82. readAllContacts();
  83. myTextView.setText(str);
  84. }
  85. // 读取所有的联系人
  86. public void readAllContacts()
  87. {
  88. ContentResolver cr = getContentResolver();
  89. Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  90. if (cur.getCount() > 0)
  91. {
  92. while (cur.moveToNext())
  93. {
  94. String rawContactsId = "";
  95. String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
  96. String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  97. str += "ContactID:" + id + "\n";
  98. // 读取rawContactsId
  99. Cursor rawContactsIdCur = cr.query(RawContacts.CONTENT_URI,
  100. null,
  101. RawContacts.CONTACT_ID +" = ?",
  102. new String[]{id}, null);
  103. if (rawContactsIdCur.moveToFirst())
  104. {
  105. rawContactsId = rawContactsIdCur.getString(rawContactsIdCur.getColumnIndex(RawContacts._ID));
  106. str += "RawContactsID:" + rawContactsId + "\n";
  107. }
  108. rawContactsIdCur.close();
  109. str += "姓名:" + name + "\n";
  110. /*
  111. Android文档:ContactsContract.CommonDataKinds.Phone:
  112. CONTENT_URI:The content:// style URI for all data records of the CONTENT_ITEM_TYPE MIME type, combined with the associated raw contact and aggregate contact data.
  113. */
  114. // 读取号码
  115. if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
  116. {
  117. //Uri phoneUri=Uri.parse("content://com.android.contacts/data/phones");
  118. // 下面的ContactsContract.CommonDataKinds.Phone.CONTENT_URI可以用phoneUri代替
  119. Cursor PhoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  120. null,
  121. //ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
  122. ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID +" = ?",
  123. new String[]{rawContactsId}, null);
  124. while (PhoneCur.moveToNext())
  125. {
  126. String number = PhoneCur.getString(PhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  127. String numberType = PhoneCur.getString(PhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
  128. str += numberType + ":" + number + "\n";
  129. }
  130. PhoneCur.close();
  131. }
  132. // 读取Email
  133. //Uri emailUri=Uri.parse("content://com.android.contacts/data/emails");
  134. // 下面的ContactsContract.CommonDataKinds.Email.CONTENT_URI可以用emailUri代替
  135. Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
  136. null,
  137. ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
  138. new String[]{id}, null);
  139. while (emailCur.moveToNext())
  140. {
  141. String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
  142. String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
  143. str += emailType + ":" + email + "\n";
  144. }
  145. /*Email类型:
  146. 1:TYPE_HOME
  147. 2:TYPE_WORK
  148. 3:TYPE_OTHER
  149. 4:TYPE_MOBILE
  150. */
  151. emailCur.close();
  152. // 读取备注
  153. String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  154. String[] noteWhereParams = new String[]{id, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
  155. Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
  156. if (noteCur.moveToFirst())
  157. {
  158. String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
  159. str += "备注:" + note + "\n";
  160. }
  161. noteCur.close();
  162. // 读取邮政地址
  163. String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  164. String[] addrWhereParams = new String[]{id,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
  165. Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);
  166. while(addrCur.moveToNext())
  167. {
  168. String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
  169. String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
  170. String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
  171. String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
  172. String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
  173. String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
  174. String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
  175. if (poBox != "")
  176. {
  177. str += "POBOX:" + poBox + "\n";
  178. }
  179. if (street != "")
  180. {
  181. str += "STREET:" + street + "\n";
  182. }
  183. if (city != "")
  184. {
  185. str += "CITY:" + city + "\n";
  186. }
  187. if (state != "")
  188. {
  189. str += "REGION:" + state + "\n";
  190. }
  191. if (postalCode != "")
  192. {
  193. str += "POSTCODE:" + postalCode + "\n";
  194. }
  195. if (country != "")
  196. {
  197. str += "COUNTRY:" + country + "\n";
  198. }
  199. if (type != "")
  200. {
  201. str += "TYPE:" + type + "\n";
  202. }
  203. }
  204. addrCur.close();
  205. // 读取即时消息
  206. String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  207. String[] imWhereParams = new String[]{id, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
  208. Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,null, imWhere, imWhereParams, null);
  209. while(imCur.moveToNext())
  210. {
  211. String imName = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
  212. String imType = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
  213. str += imType + ":" + imName + "\n";
  214. }
  215. imCur.close();
  216. // 读取公司及职位
  217. String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  218. String[] orgWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
  219. Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,null, orgWhere, orgWhereParams, null);
  220. while (orgCur.moveToNext())
  221. {
  222. String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
  223. String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
  224. str += orgName + ":" + title + "\n";
  225. }
  226. orgCur.close();
  227. str += "\n";
  228. }
  229. }
  230. }
  231. }
复制代码
*************************************
NewContactActivity:
  1. package waga.android.study.TestContacts;
  2. import android.app.Activity;
  3. import android.content.ContentUris;
  4. import android.content.ContentValues;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.provider.ContactsContract;
  8. import android.provider.ContactsContract.RawContacts;
  9. import android.provider.ContactsContract.CommonDataKinds.Email;
  10. import android.provider.ContactsContract.CommonDataKinds.Im;
  11. import android.provider.ContactsContract.CommonDataKinds.Phone;
  12. import android.provider.ContactsContract.CommonDataKinds.StructuredName;
  13. import android.provider.ContactsContract.Contacts.Data;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. public class NewContactActivity extends Activity
  18. {
  19. EditText edittext_name;
  20. EditText edittext_number;
  21. EditText edittext_email;
  22. EditText edittext_qq;
  23. Button btn_Save;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState)
  26. {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.new_contact);
  29. setTitle(R.string.menu_newContact);
  30. edittext_name = (EditText)findViewById(R.id.EditText_name);
  31. edittext_number = (EditText)findViewById(R.id.EditText_number);
  32. edittext_email = (EditText)findViewById(R.id.EditText_email);
  33. edittext_qq = (EditText)findViewById(R.id.EditText_qq);
  34. btn_Save =(Button)findViewById(R.id.Button_Save);
  35. edittext_name.setText("test-1");
  36. edittext_number.setText("13636431707-1");
  37. edittext_email.setText("[email]test1@sim.com[/email]");
  38. edittext_qq.setText("27015155-1");
  39. btn_Save.setOnClickListener(new Button.OnClickListener()
  40. {
  41. @Override
  42. public void onClick(View v)
  43. {
  44. String strName = edittext_name.getText().toString();
  45. String strNumber = edittext_number.getText().toString();
  46. String strEmail = edittext_email.getText().toString();
  47. String strQQ = edittext_qq.getText().toString();
  48. // 新建联系人
  49. insert(strName, strNumber, strEmail, strQQ);
  50. // 结束activity
  51. finish();
  52. }
  53. });
  54. }
  55. // 新建联系人
  56. public boolean insert(String given_name, String mobile_number, String work_email, String im_qq)
  57. {
  58. try
  59. {
  60. ContentValues values = new ContentValues();
  61. // 下面的操作会根据RawContacts表中已有的rawContactId使用情况自动生成新联系人的rawContactId
  62. Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
  63. long rawContactId = ContentUris.parseId(rawContactUri);
  64. // 向data表插入姓名数据
  65. if (given_name != "")
  66. {
  67. values.clear();
  68. values.put(Data.RAW_CONTACT_ID, rawContactId);
  69. values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
  70. values.put(StructuredName.GIVEN_NAME, given_name);
  71. getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
  72. }
  73. // 向data表插入电话数据
  74. if (mobile_number != "")
  75. {
  76. values.clear();
  77. values.put(Data.RAW_CONTACT_ID, rawContactId);
  78. values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
  79. values.put(Phone.NUMBER, mobile_number);
  80. values.put(Phone.TYPE, Phone.TYPE_MOBILE);
  81. getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
  82. }
  83. // 向data表插入Email数据
  84. if (work_email != "")
  85. {
  86. values.clear();
  87. values.put(Data.RAW_CONTACT_ID, rawContactId);
  88. values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
  89. values.put(Email.DATA, work_email);
  90. values.put(Email.TYPE, Email.TYPE_WORK);
  91. getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
  92. }
  93. // 向data表插入QQ数据
  94. if (im_qq != "")
  95. {
  96. values.clear();
  97. values.put(Data.RAW_CONTACT_ID, rawContactId);
  98. values.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
  99. values.put(Im.DATA, im_qq);
  100. values.put(Im.PROTOCOL, Im.PROTOCOL_QQ);
  101. getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. return false;
  107. }
  108. return true;
  109. }
  110. }
复制代码
*******************************************
DeleteContactActivity:
  1. package waga.android.study.TestContacts;
  2. import android.app.Activity;
  3. import android.content.ContentUris;
  4. import android.os.Bundle;
  5. import android.provider.ContactsContract.RawContacts;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class DeleteContactActivity extends Activity
  10. {
  11. EditText edittext_rawContactId;
  12. Button btn_Delete;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.delete_contact);
  18. setTitle(R.string.menu_deleteContact);
  19. edittext_rawContactId = (EditText)findViewById(R.id.EditText_rawContactId);
  20. btn_Delete =(Button)findViewById(R.id.Button_Delete);
  21. btn_Delete.setOnClickListener(new Button.OnClickListener()
  22. {
  23. @Override
  24. public void onClick(View v)
  25. {
  26. long rawContactId = Long.parseLong(edittext_rawContactId.getText().toString());
  27. // 新建联系人
  28. delete(rawContactId);
  29. // 结束activity
  30. finish();
  31. }
  32. });
  33. }
  34. // 删除联系人
  35. public void delete(long rawContactId)
  36. {
  37. getContentResolver().delete(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), null, null);
  38. //getContentResolver().delete(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rawContactId), null, null);
  39. }
  40. }
  41. ****************************
  42. UpdateContactActivity:
  43. package waga.android.study.TestContacts;
  44. import android.app.Activity;
  45. import android.content.ContentValues;
  46. import android.os.Bundle;
  47. import android.provider.ContactsContract;
  48. import android.provider.ContactsContract.CommonDataKinds.Phone;
  49. import android.view.View;
  50. import android.widget.Button;
  51. import android.widget.EditText;
  52. public class UpdateContactActivity extends Activity
  53. {
  54. EditText edittext_rawContactId;
  55. EditText edittext_NewNumber;
  56. Button btn_Update;
  57. @Override
  58. public void onCreate(Bundle savedInstanceState)
  59. {
  60. super.onCreate(savedInstanceState);
  61. setContentView(R.layout.update_contact);
  62. setTitle(R.string.menu_updateContact);
  63. edittext_rawContactId = (EditText)findViewById(R.id.EditText_rawContactId_Delete);
  64. edittext_NewNumber = (EditText)findViewById(R.id.EditText_newNumber);
  65. btn_Update =(Button)findViewById(R.id.Button_Update);
  66. btn_Update.setOnClickListener(new Button.OnClickListener()
  67. {
  68. @Override
  69. public void onClick(View v)
  70. {
  71. String strRawContactId = edittext_rawContactId.getText().toString();
  72. String strNewNumber = edittext_NewNumber.getText().toString();
  73. // 更新联系人
  74. update(strRawContactId, strNewNumber);
  75. // 结束activity
  76. finish();
  77. }
  78. });
  79. }
  80. // 更新联系人
  81. public void update(String rawRawContactId, String NewNumber)
  82. {
  83. ContentValues values = new ContentValues();
  84. values.put(Phone.NUMBER, NewNumber);
  85. values.put(Phone.TYPE, Phone.TYPE_MOBILE);
  86. String Where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  87. String[] WhereParams = new String[]{rawRawContactId, Phone.CONTENT_ITEM_TYPE, };
  88. getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, Where, WhereParams);
  89. }
  90. }
复制代码

转载于:https://www.cnblogs.com/517zd/archive/2012/10/02/2710600.html

你可能感兴趣的文章
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
求出斐波那契数组
查看>>
Vue.js 基础学习之组件通信
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
每天一个Linux命令 - 【chkconfig】
查看>>
△UVA10106 - Product(大数乘法)
查看>>
golang (7) 文件操作
查看>>
关于 Object.defineProperty()
查看>>
[转] Maven 从命令行获取项目的版本号
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
Java Development Environment in Linux: Install and Configure Oracle
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>