Android通讯录实现联系人模糊查找

简单粗暴,直接上代码!这段代码是上课时候做实验的时候写的,通过调用系统的ContentProvider实现联系人的模糊查找。
// 模糊查找联系人
		bt_fu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String name=null,number=null,email=null;
				ContentResolver cr = getContentResolver();
				String projection[] = { ContactsContract.Contacts.DISPLAY_NAME,
						ContactsContract.CommonDataKinds.Phone.NUMBER
						};
				Cursor cursor = cr.query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						projection, ContactsContract.Contacts.DISPLAY_NAME
								+ " like " + "'%"
								+ et_name.getText().toString()+ "%'", null,
						null);
				Cursor cursor2 = cr.query(
						ContactsContract.CommonDataKinds.Email.CONTENT_URI,
						projection, ContactsContract.Contacts.DISPLAY_NAME
								+ " like " + "'%"
								+ et_name.getText().toString()+ "%'", null,
						null);
				while (cursor.moveToNext()) {
					name = cursor.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
					number = cursor.getString(cursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					while(cursor2.moveToNext()){
						email = cursor2.getString(cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
					}
					
				}
				Log.i("------查询到的此条记录为:--------", name + "   " + number +"  "+email
						);
				cursor.close();
			}
		});

你可能感兴趣的:(Android)