API LV5以上使用ContactsContract.Contacts来获得联系人姓名和号码。
跟之前的相比,姓名获取不变,但是获取号码方法发生改变。
package ice.hui;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG="PhoneContentProvider";
private int name;
private int number;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String []requestColumns ={
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID
};
Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, requestColumns, null, null, null);
name = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
cur.moveToFirst();
while(!cur.isAfterLast()){
Log.i(TAG,"name= "+cur.getString(name));
String phoneid = cur.getString((cur.getColumnIndex(ContactsContract.Contacts._ID)));
Cursor phone = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+phoneid, null, null);
number = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phone.moveToFirst();
while(!phone.isAfterLast()){
Log.i(TAG,"phone= "+phone.getString(number));
phone.moveToNext();
}
cur.moveToNext();
}
}
}
由此可见,要获取号码首先要获取联系人的ID,再通过ID获取相对应的号码,要注意号码可能不止一个,所以在里面需要一个判断。