Android Contacts(一)—— 读取联系人

分类:Android 3509人阅读 评论(0) 收藏 举报 android listview string sqlite list null

Introduction To Android Contacts

Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See theAndroid SQLite and Cursor Articlefor more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.


Contacts 读取代码:

[java] view plain copy print ?
  1. packagecom.homer.phone;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importandroid.app.Activity;
  5. importandroid.database.Cursor;
  6. importandroid.os.Bundle;
  7. importandroid.provider.ContactsContract;
  8. importandroid.provider.ContactsContract.CommonDataKinds.Phone;
  9. importandroid.widget.ListView;
  10. importandroid.widget.SimpleAdapter;
  11. publicclassphoneReadextendsActivity{
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. showListView();
  16. }
  17. privatevoidshowListView(){
  18. ListViewlistView=newListView(this);
  19. ArrayList<HashMap<String,String>>list=getPeopleInPhone2();
  20. SimpleAdapteradapter=newSimpleAdapter(
  21. this,
  22. list,
  23. android.R.layout.simple_list_item_2,
  24. newString[]{"peopleName","phoneNum"},
  25. newint[]{android.R.id.text1,android.R.id.text2}
  26. );
  27. listView.setAdapter(adapter);
  28. setContentView(listView);
  29. }
  30. privateArrayList<HashMap<String,String>>getPeopleInPhone2(){
  31. ArrayList<HashMap<String,String>>list=newArrayList<HashMap<String,String>>();
  32. Cursorcursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);//获取手机联系人
  33. while(cursor.moveToNext()){
  34. HashMap<String,String>map=newHashMap<String,String>();
  35. intindexPeopleName=cursor.getColumnIndex(Phone.DISPLAY_NAME);//peoplename
  36. intindexPhoneNum=cursor.getColumnIndex(Phone.NUMBER);//phonenumber
  37. StringstrPeopleName=cursor.getString(indexPeopleName);
  38. StringstrPhoneNum=cursor.getString(indexPhoneNum);
  39. map.put("peopleName",strPeopleName);
  40. map.put("phoneNum",strPhoneNum);
  41. list.add(map);
  42. }
  43. if(!cursor.isClosed()){
  44. cursor.close();
  45. cursor=null;
  46. }
  47. returnlist;
  48. }
  49. }

AndroidManifest.xml 权限

记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission

<uses-permission android:name="android.permission.READ_CONTACTS" />


运行结果:



代码示例


Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联

分类:Android 4019人阅读 评论(3) 收藏 举报 sms android string null date 手机

Android 的SMS读取短信,可以获取发信人/收信人的手机号码(address),Contacts的联系人,可以过滤手机号码(address),因此SMS可以通过手机号码(address)关联到Contacts联系人


SMS - Contacts 关联代码

[java] view plain copy print ?
  1. //通过address手机号关联Contacts联系人的显示名字
  2. privateStringgetPeopleNameFromPerson(Stringaddress){
  3. if(address==null||address==""){
  4. return"(noaddress)\n";
  5. }
  6. StringstrPerson="null";
  7. String[]projection=newString[]{Phone.DISPLAY_NAME,Phone.NUMBER};
  8. Uriuri_Person=Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,address);//address手机号过滤
  9. Cursorcursor=getContentResolver().query(uri_Person,projection,null,null,null);
  10. if(cursor.moveToFirst()){
  11. intindex_PeopleName=cursor.getColumnIndex(Phone.DISPLAY_NAME);
  12. StringstrPeopleName=cursor.getString(index_PeopleName);
  13. strPerson=strPeopleName;
  14. }
  15. cursor.close();
  16. returnstrPerson;
  17. }


SMS - Contacts 关联示例代码:

[java] view plain copy print ?
  1. packagecom.homer.phone;
  2. importjava.sql.Date;
  3. importjava.text.SimpleDateFormat;
  4. importandroid.app.Activity;
  5. importandroid.database.Cursor;
  6. importandroid.database.sqlite.SQLiteException;
  7. importandroid.net.Uri;
  8. importandroid.os.Bundle;
  9. importandroid.provider.ContactsContract;
  10. importandroid.provider.ContactsContract.CommonDataKinds.Phone;
  11. importandroid.util.Log;
  12. importandroid.widget.ScrollView;
  13. importandroid.widget.TextView;
  14. publicclassphoneRead2extendsActivity{
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. TextViewtv=newTextView(this);
  19. tv.setText(getSmsInPhone());
  20. ScrollViewsv=newScrollView(this);
  21. sv.addView(tv);
  22. setContentView(sv);
  23. }
  24. publicStringgetSmsInPhone(){
  25. finalStringSMS_URI_ALL="content://sms/";
  26. finalStringSMS_URI_INBOX="content://sms/inbox";
  27. finalStringSMS_URI_SEND="content://sms/sent";
  28. finalStringSMS_URI_DRAFT="content://sms/draft";
  29. finalStringSMS_URI_OUTBOX="content://sms/outbox";
  30. finalStringSMS_URI_FAILED="content://sms/failed";
  31. finalStringSMS_URI_QUEUED="content://sms/queued";
  32. StringBuildersmsBuilder=newStringBuilder();
  33. try{
  34. Uriuri=Uri.parse(SMS_URI_ALL);
  35. String[]projection=newString[]{"_id","address","person","body","date","type"};
  36. Cursorcur=getContentResolver().query(uri,projection,null,null,"datedesc");//获取手机内部短信
  37. if(cur.moveToFirst()){
  38. intindex_Address=cur.getColumnIndex("address");
  39. intindex_Person=cur.getColumnIndex("person");
  40. intindex_Body=cur.getColumnIndex("body");
  41. intindex_Date=cur.getColumnIndex("date");
  42. intindex_Type=cur.getColumnIndex("type");
  43. do{
  44. StringstrAddress=cur.getString(index_Address);
  45. intintPerson=cur.getInt(index_Person);
  46. Stringstrbody=cur.getString(index_Body);
  47. longlongDate=cur.getLong(index_Date);
  48. intintType=cur.getInt(index_Type);
  49. SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
  50. Dated=newDate(longDate);
  51. StringstrDate=dateFormat.format(d);
  52. StringstrType="";
  53. if(intType==1){
  54. strType="接收";
  55. }elseif(intType==2){
  56. strType="发送";
  57. }else{
  58. strType="null";
  59. }
  60. StringstrAddress2=getPeopleNameFromPerson(strAddress);
  61. smsBuilder.append("[");
  62. //smsBuilder.append(strAddress+",");
  63. smsBuilder.append(strAddress+":"+strAddress2+",");
  64. smsBuilder.append(intPerson+",");
  65. smsBuilder.append(strbody+",");
  66. smsBuilder.append(strDate+",");
  67. smsBuilder.append(strType);
  68. smsBuilder.append("]\n\n");
  69. }while(cur.moveToNext());
  70. if(!cur.isClosed()){
  71. cur.close();
  72. cur=null;
  73. }
  74. }else{
  75. smsBuilder.append("noresult!");
  76. }//endif
  77. smsBuilder.append("getSmsInPhonehasexecuted!");
  78. }catch(SQLiteExceptionex){
  79. Log.d("SQLiteExceptioningetSmsInPhone",ex.getMessage());
  80. }
  81. returnsmsBuilder.toString();
  82. }
  83. //通过address手机号关联Contacts联系人的显示名字
  84. privateStringgetPeopleNameFromPerson(Stringaddress){
  85. if(address==null||address==""){
  86. return"(noaddress)\n";
  87. }
  88. StringstrPerson="null";
  89. String[]projection=newString[]{Phone.DISPLAY_NAME,Phone.NUMBER};
  90. Uriuri_Person=Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,address);//address手机号过滤
  91. Cursorcursor=getContentResolver().query(uri_Person,projection,null,null,null);
  92. if(cursor.moveToFirst()){
  93. intindex_PeopleName=cursor.getColumnIndex(Phone.DISPLAY_NAME);
  94. StringstrPeopleName=cursor.getString(index_PeopleName);
  95. strPerson=strPeopleName;
  96. }
  97. cursor.close();
  98. returnstrPerson;
  99. }
  100. }

AndroidManifest.xml 权限

记得在AndroidManifest.xml中加入android.permission.READ_SMSandroid.permission.READ_CONTACTS这两个permission

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />


运行结果:



示例代码



更多相关文章

  1. FragmentTabhost记录
  2. android 短信验证自动获取验证码
  3. Android获取本机电话号码的简单方法
  4. 获取Android的Java源代码并在Eclipse中关联查看的最新方法
  5. Android联系人数据库全
  6. Android(安卓)SMS(一) —— 读取短信
  7. Android(安卓)发送短信 sms
  8. android获取手机中的短信和,android获取手机通讯录联系人和号码
  9. Android之使用ContentResolver对通信录中的数据进行简单操作

随机推荐

  1. Android客户端与服务器的数据交互总结
  2. Android多媒体学习一:Android中Image的简
  3. android手机通过串口蓝牙透传模块与AVR单
  4. android init进程说明
  5. Android(安卓)MediaRecorder视频录制,多分
  6. android区分pad还是phone设备
  7. android 桌面程序 滑动抽屉 SlidingDraw,
  8. Android命令行下运行JAVA程序之StatusBar
  9. android实现蓝牙耳机的连接及列表的管理
  10. Android源代码分析(三) MediaScanner源码