2012年8月17日 星期五
ContactsContract-Phone Books
****************
不要忘了在 Manifest.xml 加上: <uses-permission android:name="android.permission.READ_CONTACTS"
*******************************************************
public class PhoneContactActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1;
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Uri uri2 = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String result="ID\t 名字\t\t 電話\n";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.tv1);
ContentResolver cr = getContentResolver();
Cursor c = cr.query(uri, null, null, null, null);
while (c.moveToNext()) {
// 取得聯絡人ID
//int contactColumnID = c.getColumnIndex(PhoneLookup._ID);
int contactColumnID = c.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = c.getString(contactColumnID);
result += contactID + "\t";
// 取得聯絡人名字
int contactNameColumnIndex = c
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String contactName = c.getString(contactNameColumnIndex);
result += " " + contactName + "\t\n";
// 準備 phoneCursor 儲存所有人的所有電話,條件是每人的所有電話=每個人的ID號碼
Cursor phoneCursor = cr.query(uri2, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactID, null, null);
// 取得電話號碼(可能每個人有超過一個電話號碼)
while (phoneCursor.moveToNext()) {
int contactNumberColumn = phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumStr = phoneCursor.getString(contactNumberColumn);
result += (" : " + phoneNumStr) + "\n";
}
result += "\n";
phoneCursor.close();
}
c.close();
tv1.setText(result);
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言