使用内容提供器获取系统联系人

作者: 大话程序 | 来源:发表于2016-05-16 08:39 被阅读266次

系统联系人是系统中共享出来的数据,可以通过内容提供器读取出来

  • 编写布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/contacts_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

  • 编写使用现有的内容提供器来读取系统联系人
public class MainActivity extends Activity {
    //ListView控件,来显示联系人列表
    ListView contactsView;
    //LIstView的适配器
    ArrayAdapter<String> adapter;
    //存储联系人的集合
    List<String> contactsList = new ArrayList<String>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contactsView = (ListView) findViewById(R.id.contacts_view);
        //读取系统联系人
        readContacts();
        //创建适配器
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList);
        //为ListView适配适配器
        contactsView.setAdapter(adapter);
    }

    private void readContacts() {
        Cursor cursor = null;
        try {
            //查询联系人
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, null, null, null);
            while (cursor.moveToNext()) {
                //获取联系人姓名
                String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                //获取联系人手机号
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactsList.add(displayName + "\n" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (cursor != null) {
                cursor.close();
            }
        }
    }

}

ContactsContract.CommonDataKinds.Phone类已经帮我们做好了封装,提供了一个CONTENT_URI常量,而这个常量就是使用Uri.parse() 解析出来的
联系人姓名这一列对应的常量是ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
联系人手机号对应的常量是ContactsContract.CommonDataKinds.Phone.NUMBER

相关文章

  • 使用内容提供器获取系统联系人

    系统联系人是系统中共享出来的数据,可以通过内容提供器读取出来 编写布局文件 编写使用现有的内容提供器来读取系统联系...

  • Android四大组件——ContentProvider

    ContentProvider的作用 ①使用Android提供的API访问系统程序中的数据,如获取手机联系人的数据...

  • 组件三:内容提供器Contain Provider

    内容提供器可以选择只对一部分数据进行共享 1 类型 (1)使用现有的内容提供器(一些系统应用提供的)读取和操作相应...

  • Android获取手机通讯录-根据排序方式进行

    在英文模式下可以进行更改联系人设置,由于安卓系统已经将获取联系人排序方式这个取消掉了,所以部分安卓手机也就没有提供...

  • iOS通讯录开发

    场景一:直接选择一个联系人的电话号码 这里不需要先获取所有的联系人自己做联系人列表,直接使用系统自带的Addres...

  • iOS 系统媒体音量

    iOS 媒体音量获取以及控制 获取系统音量 调整系统音量 iOS7 之后,可以使用系统提供的MPVolumeVie...

  • Android跨程序共享数据,探究内容提供器(进阶篇)

    上一章讲到怎么使用ContentResolver访问系统提供的数据接口读取联系人信息,那么系统程序是怎样对外建立并...

  • 内容提供器

    1.ContentResolver  对于每一个应用程序来说,如果想要访问内容提供器(ContentProvide...

  • 内容提供器

    Content Provider 主要用于在不同App间共享数据。 权限申请 App中某些功能的执行需要申请And...

  • 内容提供器

    一般有两种,一种是使用现有的内容提供器,读取和操作相应程序的数据,另一种是创建自己的内容提供器给其他程序提供数据。...

网友评论

    本文标题:使用内容提供器获取系统联系人

    本文链接:https://www.haomeiwen.com/subject/cycbrttx.html