使用Content Provider得到联系人信息实例

转自:http://blog.csdn.net/wangkuifeng0118/article/details/7023642

ContentProvider简介

  

  我们说Android应用程序的四个核心组件是:Activity、Service、BroadcastReceiver和ContentProvider。在Android中,应用程序彼此之间相互独立的,它们都运行在自己独立的虚拟机中。ContentProvider提供了程序之间共享数据的方法,一个程序可以使用ContentProvider定义一个URI,提供统一的操作接口,其他程序可以通过此URI访问指定的数据,进行数据的增、删、改、查。

        废话不多说,下面来看一个ContentProvider访问联系人信息的demo,

       首先建立一个ContectsDemo的android项目:

      

      接下来看一下main.xml:

    

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/text"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/button1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="获取联系人信息" />  
  18.   
  19. </LinearLayout>  

       然后看一下主程序:

   

[java]  view plain copy
  1. public class ContectsDemoActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private Button button1;  
  4.     private TextView text;  
  5.       
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.          
  11.           
  12.         text=(TextView) this.findViewById(R.id.text);  
  13.         button1=(Button) this.findViewById(R.id.button1);  
  14.         button1.setOnClickListener(new View.OnClickListener() {  
  15.               
  16.             public void onClick(View v) {  
  17.                 // TODO Auto-generated method stub  
  18.                  
  19.                 StringBuilder sb=getContacts();  
  20.                 text.setText(sb.toString());  
  21.                   
  22.             }  
  23.         });  
  24.     }  
  25.       
  26.     private StringBuilder getContacts() {    
  27.          StringBuilder  sbLog = new StringBuilder();    
  28.         // 得到ContentResolver对象       
  29.         ContentResolver cr = this.getContentResolver();         
  30.         // 取得电话本中开始一项的光标,主要就是查询"contacts"表    
  31.         Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, nullnullnullnull);  
  32.         if(!cursor.moveToFirst()){  
  33.             sbLog.append("获取内容为空!");  
  34.             return sbLog;  
  35.         }  
  36.        if(cursor.moveToFirst())       
  37.         {       
  38.            
  39.                 
  40.             // 取得联系人名字 (显示出来的名字),实际内容在 ContactsContract.Contacts中    
  41.            int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);       
  42.            String name = cursor.getString(nameIndex);    
  43.            sbLog.append("name=" + name + ";");    
  44.                 
  45.             // 取得联系人ID    
  46.             String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));       
  47.                 
  48.             // 根据联系人ID查询对应的电话号码    
  49.             Cursor phoneNumbers = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "      
  50.                     + contactId, nullnull);                 
  51.             // 取得电话号码(可能存在多个号码)       
  52.             while (phoneNumbers.moveToNext())       
  53.             {    
  54.                 String strPhoneNumber = phoneNumbers.getString(phoneNumbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));       
  55.                 sbLog.append("Phone=" + strPhoneNumber + ";");    
  56.             }       
  57.             phoneNumbers.close();     
  58.                 
  59.             // 根据联系人ID查询对应的email    
  60.             Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "      
  61.                     + contactId, nullnull);                 
  62.             // 取得email(可能存在多个email)       
  63.             while (emails.moveToNext())       
  64.             {    
  65.                 String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));       
  66.                 sbLog.append("Email=" + strEmail + ";");    
  67.             }       
  68.             emails.close();     
  69.                 
  70.         }    
  71.         cursor.close();    
  72.         Log.e("-------------------", sbLog.toString());  
  73.         return sbLog;  
  74.     }    
  75.      
  76. }  

       最后不要忘了加访问权限:

    

[html]  view plain copy
  1. <uses-permission android:name="android.permission.READ_CONTACTS" />  
  2.   
  3.    <uses-permission android:name="android.permission.WRITE_CONTACTS" />   

        最后运行,看一下效果:

       

       点击获取联系人按钮看有什么效果:

       

       这样就得到了我们联系人的名字,电话号和email了。为了证明让我们看一下我模拟器里的通讯录信息:

     

       OK!   cursor.moveToNext()  可以获取多条 ,在这里我就不多附属了,而且既然可以读联系人信息,就可以通过ContentProvider添加联系人信息,以后慢慢复述。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值