本文共 4780 字,大约阅读时间需要 15 分钟。
读取系统联系人并实现联系人信息回显
在本文中,详细介绍了如何在Android应用中读取系统联系人信息,并实现联系人信息的回显功能。以下将分为两大部分进行阐述:第一部分介绍如何读取系统联系人信息,第二部分介绍如何实现联系人信息的回显。
在Android系统中,联系人信息是由系统提供的数据,通常存储在两个主要数据库表中:raw_contacts 和 data。通过使用ContentResolver和Cursor进行数据库查询,可以有效地读取联系人信息。
ContentResolver是Android系统中用于与内容提供者进行交互的核心接口。它能够解析不同内容提供者的数据,返回适用于应用的数据格式。
raw_contacts 表:
contact_id:联系人唯一性标识符。data 表:
raw_contact_id:外键,用于关联raw_contacts表。data1:存储联系人信息的字段,包含电话号码和联系人名称。mimetype_id:表示数据类型的引用。mimetypes 表:
vnd.android.cursor.item/phone_v2,联系人名称对应的类型为 vnd.android.cursor.item/name。通过ContentResolver进行查询,常用的URI包括:
content://com.android.contacts/raw_contactscontent://com.android.contacts/data
raw_contacts表中的contact_id。contact_id,查询data表,获取data1和mimetype字段。mimetype类型,解析数据,填充到一个哈希表中。在用户点击联系人条目时,需要实现信息的回显。例如,双击第一个条目后,电话号码自动添加。
private ListView lv_contact;private List> contactList;private MyAdapter mAdapter;private Handler mHandler;private EditText et_phone_number;private Button bt_select_number;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact_list); initUI(); initData();}private void initUI() { lv_contact = (ListView) findViewById(R.id.lv_contact); lv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { if (mAdapter != null) { HashMap hashMap = mAdapter.getItem(i); String phone = hashMap.get("phone"); Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(0, intent); finish(); } } }); et_phone_number = (EditText) findViewById(R.id.et_phone_number); bt_select_number = (Button) findViewById(R.id.bt_select_number); bt_select_number.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getApplicationContext(), ContactListActivity.class); startActivityForResult(intent, 0); } });}private void initData() { new Thread() { @Override public void run() { ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query( Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null ); contactList.clear(); while (cursor.moveToNext()) { String id = cursor.getString(0); Cursor indexCursor = contentResolver.query( Uri.parse("content://com.android.contacts/data"), new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{id}, null ); HashMap hashMap = new HashMap<>(); while (indexCursor.moveToNext()) { String data = indexCursor.getString(0); String type = indexCursor.getString(1); if (type.equals("vnd.android.cursor.item/phone_v2")) { if (!TextUtils.isEmpty(data)) { hashMap.put("phone", data); } } else if (type.equals("vnd.android.cursor.item/name")) { if (!TextUtils.isEmpty(data)) { hashMap.put("name", data); } } } contactList.add(hashMap); indexCursor.close(); } cursor.close(); mHandler.sendEmptyMessage(0); } }.start();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data != null) { String phone = data.getStringExtra("phone"); phone = phone.replace("-", "").replace(" ", "").trim(); et_phone_number.setText(phone); SpUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone); } super.onActivityResult(requestCode, resultCode, data);}
通过以上方法,可以实现读取系统联系人信息并实现信息的回显。这种方法不仅满足了基本需求,还通过优化用户体验和数据存储,使得应用更具实用性。这篇文章详细介绍了实现过程中关键的步骤和细节,希望对开发者有所帮助。
转载地址:http://cxgfk.baihongyu.com/