Android 获取联系人的例子
By:Roy.LiuLast updated:2014-10-28
用Android,经常会获取电话里面保存的联系人信息,可以得到名称,电话号码,以及邮件等,根据这些信息,可以匹配一些东西,比如微信绑定电话之后,很自然的,微信会提示你XX人有微信,是否加为好友,这类的例子很多,也不必多说,都死Android的基础知识。只是我这样的小白才关注的东西,不过也好,自己收集一些程序学习还是可以的。
下面这个例子,附带源码,就是一个得到联系人列表的App, 其实代码很简单。
布局代码:
AndroidManifest.xml 代码,注意权限的设置
Activity 代码如下:
运行之后,截图如下:
整个程序代码如下:
android 获取联系人列表例子
下面这个例子,附带源码,就是一个得到联系人列表的App, 其实代码很简单。
布局代码:
AndroidManifest.xml 代码,注意权限的设置
Activity 代码如下:
import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.widget.TextView; public class MainActivity extends Activity { public TextView outputText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); outputText = (TextView) findViewById(R.id.textView1); fetchContacts(); } public void fetchContacts() { String phoneNumber = null; String email = null; Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; String _ID = ContactsContract.Contacts._ID; String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI; String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID; String DATA = ContactsContract.CommonDataKinds.Email.DATA; StringBuffer output = new StringBuffer(); ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null); // Loop for every contact in the phone if (cursor.getCount() > 0) { while (cursor.moveToNext()) { String contact_id = cursor.getString(cursor.getColumnIndex( _ID )); String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME )); int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER ))); if (hasPhoneNumber > 0) { output.append("\n First Name:" + name); // Query and loop for every phone number of the contact Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null); while (phoneCursor.moveToNext()) { phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); output.append("\n Phone number:" + phoneNumber); } phoneCursor.close(); // Query and loop for every email of the contact Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null); while (emailCursor.moveToNext()) { email = emailCursor.getString(emailCursor.getColumnIndex(DATA)); output.append("\nEmail:" + email); } emailCursor.close(); } output.append("\n"); } outputText.setText(output); } } }
运行之后,截图如下:
整个程序代码如下:
android 获取联系人列表例子
From:一号门
Previous:java 连接 sqlite 的一个例子。
COMMENTS