Android通过ContentProvider取得电话本的数据

移动开发 Android
这里介绍通过ContentProvider取得电话本的数据的方法,先通过getContentResolver方法来取得一个ContentResolver对象,然后通过其query方法查询出符合标准的电话本记录,最后将这些数据都显示在一个TextView中即可。

ContentProvider为存储和获取数据提供了统一的接口。ContentProvide对数据进行封装,不用关心数据存储的细节。使用表的形式来组织数据。

这里介绍通过ContentProvider取得电话本的数据的方法,先通过getContentResolver方法来取得一个ContentResolver对象,然后通过其query方法查询出符合标准的电话本记录,最后将这些数据都显示在一个TextView中即可。

public class Activity01 extends Activity 

    public void onCreate(Bundle savedInstanceState){ 
        TextView tv = new TextView(this); 
        String string = ""
        super.onCreate(savedInstanceState); 
        //得到ContentResolver 对象 
        ContentResolver cr = getContentResolver(); 
        //取得电话本中开始一项的光标 
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, nullnullnullnull); 
        //向下移动光标 
        while(cursor.moveToNext()){ 
            //取得联系人名字 
            int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
            String contact = cursor.getString(nameFieldColumnIndex); 
            //取得电话号码 
            int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); 
            String number = cursor.getString(numberFieldColumnIndex); 
            string += (contact+":"+number+"\n"); 
        } 
         cursor.close(); 
        //设置TextView 显示的内容 
        tv.setText(string); 
        //显示到屏幕 
        setContentView(tv); 
    } 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

TextView设置中的代码如下:

<uses-permission 
android:name="android.permission.READ_CONTACTS"> 
</uses-permission> 
  • 1.
  • 2.
  • 3.
责任编辑:徐川 来源: OSChina
相关推荐

2014-11-14 09:50:39

微信电话本VoLTE

2013-08-20 09:49:43

2014-11-19 10:16:10

微信电话本运营商

2021-10-20 22:06:04

微信电话本移动应用

2014-12-18 16:22:44

华为

2023-03-30 08:26:31

DNSTCPUDP

2023-11-07 11:17:25

Android数据共享

2014-11-20 13:34:17

微信网易有道

2010-02-05 17:25:10

电话交换机

2014-07-29 15:57:01

ContentProv

2014-11-20 13:05:18

微信网易有道

2011-03-11 09:41:53

2009-03-24 08:33:14

AndroidGoogle移动os

2012-07-27 09:32:36

金山WPSWPS移动版

2015-04-13 10:18:37

VoLTE4G话音

2014-11-20 13:24:35

Android 5.0Google

2009-03-26 08:28:17

AndroidGoogle移动OS

2014-03-19 13:51:14

微信专利技术

2021-03-01 08:20:06

AndroidFileProvideContentProv

2014-07-10 15:51:53

点赞
收藏

51CTO技术栈公众号