读取系统联系人
系统联系人提供了一个内容提供者,通过内容解析器,匹配Url地址
1,内容解析器
2,Url地址,查看系统联系人数据库,内容提供者源码
先看清单文件
后看java类(联系人数据库有多张表)
contents://com.android.contacts/表名
3,系统联系人数据库中核心表,表结构
raw_contacts 联系人表: contact_id 联系人唯一性id值
data 用户信息表:raw_contact_id作为外键,和raw_contacts中contact_id做关联查询
获取data1字段,包含了电话号码以及联系人名称
mimetype_id字段,包含了当前行data1对应的数据类型
mimetypes 类型表: 获取data表中mimetype_id和mimetypes中_id做关联查询,获取指向的信息类型
电话号码:vnd.android.cursor.item/phone_v2
用户名称:vnd.android.cursor.item/name
google,data表和mimetypes生成了一个视图
4,表的访问方式
content://com.android.contacts/raw_contacts
content://com.android.contacts/data
public class ContactListActivity extends Activity { protected static final String tag = "ContactListActivity"; private ListView lv_contact; private List<HashMap<String,String>> contactList = new ArrayList<HashMap<String,String>>(); private MyAdapter mAdapter; private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { //8,填充数据适配器 mAdapter = new MyAdapter(); lv_contact.setAdapter(mAdapter); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact_list); initUI(); initData(); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return contactList.size(); } @Override public HashMap<String, String> getItem(int position) { return contactList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(getApplicationContext(), R.layout.listview_contact_item, null); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone); tv_name.setText(getItem(position).get("name")); tv_phone.setText(getItem(position).get("phone")); return view; } } /** * 获取系统联系人数据方法 */ private void initData() { //因为读取系统联系人,可能是一个耗时操作,放置到子线程中处理 new Thread(){ public void run() { //1,获取内容解析器对象 ContentResolver contentResolver = getContentResolver(); //2,做查询系统联系人数据库表过程(读取联系人权限) Cursor cursor = contentResolver.query( Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null); contactList.clear(); //3,循环游标,直到没有数据为止 while(cursor.moveToNext()){ String id = cursor.getString(0); // Log.i(tag, "id = "+id); //4,根据用户唯一性id值,查询data表和mimetype表生成的视图,获取data以及mimetype字段 Cursor indexCursor = contentResolver.query( Uri.parse("content://com.android.contacts/data"), new String[]{"data1","mimetype"}, "raw_contact_id = ?", new String[]{id}, null); //5,循环获取每一个联系人的电话号码以及姓名,数据类型 HashMap<String, String> hashMap = new HashMap<String, String>(); while(indexCursor.moveToNext()){ String data = indexCursor.getString(0); String type = indexCursor.getString(1); //6,区分类型去给hashMap填充数据 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); } } } indexCursor.close(); contactList.add(hashMap); } cursor.close(); //7,消息机制,发送一个空的消息,告知主线程可以去使用子线程已经填充好的数据集合 mHandler.sendEmptyMessage(0); }; }.start(); } private void initUI() { lv_contact = (ListView) findViewById(R.id.lv_contact); lv_contact.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { //1,获取点中条目的索引指向集合中的对象 if(mAdapter!=null){ HashMap<String, String> hashMap = mAdapter.getItem(position); //2,获取当前条目指向集合对应的电话号码 String phone = hashMap.get("phone"); //3,此电话号码需要给第三个导航界面使用 //4,在结束此界面回到前一个导航界面的时候,需要将数据返回过去 Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(0, intent); finish(); } } }); } }
public class Setup3Activity extends Activity { private EditText et_phone_number; private Button bt_select_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setup3); initUI(); } private void initUI() { //显示电话号码的输入框 et_phone_number = (EditText) findViewById(R.id.et_phone_number); //获取联系人电话号码回显过程 String phone = SpUtil.getString(this, ConstantValue.CONTACT_PHONE, ""); et_phone_number.setText(phone); //点击选择联系人的对话框 bt_select_number = (Button) findViewById(R.id.bt_select_number); bt_select_number.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), ContactListActivity.class); startActivityForResult(intent, 0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(data!=null){ //1,返回到当前界面的时候,接受结果的方法 String phone = data.getStringExtra("phone"); //2,将特殊字符过滤(中划线转换成空字符串) phone = phone.replace("-", "").replace(" ", "").trim(); et_phone_number.setText(phone); //3,存储联系人至sp中 SpUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone); } super.onActivityResult(requestCode, resultCode, data); } public void nextPage(View view){ //点击按钮以后,需要获取输入框中的联系人,再做下一页操作 String phone = et_phone_number.getText().toString(); //在sp存储了相关联系人以后才可以跳转到下一个界面 // String contact_phone = SpUtil.getString(getApplicationContext(), ConstantValue.CONTACT_PHONE, ""); if(!TextUtils.isEmpty(phone)){ Intent intent = new Intent(getApplicationContext(), Setup4Activity.class); startActivity(intent); finish(); //如果现在是输入电话号码,则需要去保存 SpUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone); overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim); }else{ ToastUtil.show(this,"请输入电话号码"); } } public void prePage(View view){ Intent intent = new Intent(getApplicationContext(), Setup2Activity.class); startActivity(intent); finish(); overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim); } }