Android掉系统通讯录并返回姓名电话

来源:互联网 发布:ceic数据库与wind 编辑:程序博客网 时间:2024/05/01 04:52

1.打开系统通讯录

Intent intent = new Intent(Intent.ACTION_PICK,android.provider.ContactsContract.Contacts.CONTENT_URI);startActivityForResult(intent, 1);

2.接收返回值

Uri uri = data.getData();String string = "";        // 得到ContentResolver对象ContentResolver cr = getContentResolver();// 取得电话本中开始一项的光标Cursor cursor = cr.query(uri, null, null, null, null);// 向下移动光标while (cursor.moveToNext()) {    // 取得联系人名字    int nameFieldColumnIndex = cursor            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);    String contact = cursor.getString(nameFieldColumnIndex);    //如果值为1,则该联系人至少有一个电话号码;如果值为0,则该联系人没有电话号码     String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));    Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);    while(phone.moveToNext())    {        String Number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));        string += (contact + ":" + Number + " ");    }    Toast.makeText(NewsActivity.this,string,Toast.LENGTH_LONG).show();}

0 0