直接上源碼:
package com.example.contact;<br />import android.app.Activity;<br />import android.content.ContentResolver;<br />import android.database.Cursor;<br />import android.os.Bundle;<br />import android.provider.ContactsContract;<br />import android.provider.ContactsContract.PhoneLookup;<br />import android.util.Log;<br />import android.widget.TextView;<br />public class ReadContack extends Activity {<br /> /** Called when the activity is first created. */<br />@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> TextView tv = new TextView(this);<br /> String string="";<br /> super.onCreate(savedInstanceState);<br /> ContentResolver cr = getContentResolver();<br /> Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,<br /> null, null, null, null);<br /> while(cursor.moveToNext()) {<br /> int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);<br /> String contact = cursor.getString(nameFieldColumnIndex);</p><p> string += contact;<br /> string += ":";<br /> //下面為新的擷取連絡人電話號碼方法<br /> //擷取連絡人的ID號<br /> String contactid = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));<br /> Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,<br /> ContactsContract.CommonDataKinds.Phone.CONTACT_ID+'='+contactid,<br /> null, null);<br /> //電話號碼可以是幾組<br /> while(phone.moveToNext()) {<br /> String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));<br /> string += strPhoneNumber;<br /> }<br /> string += "/n";<br /> phone.close();<br /> }<br /> cursor.close();<br /> tv.setText(string);<br /> setContentView(tv);<br /> }<br />}