android:contact

package com.example.contact;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.test.AndroidTestCase;
import android.util.Log;

public class ContactTest extends AndroidTestCase {
	private static final String TAG="ContactTest";
	
      public void testGetAllContact()throws Throwable{
    	  Uri uri=ContactsContract.Contacts.CONTENT_URI;
    	  ContentResolver contentResolver=this.getContext().getContentResolver();
    	  Cursor cursor=contentResolver.query(uri, null, null, null, null);
    	  while(cursor.moveToNext()){
    		  StringBuilder str=new StringBuilder();
    		  String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    		  String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    		  str.append("contactId").append(contactId).append("name").append(name);
    		  Cursor phones=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
    		  while(phones.moveToNext()){
    			  String phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    			  str.append(",phone").append(phoneNumber);
    		  }
    		  phones.close();
    		  Log.i(TAG,str.toString());
    	  }
    	  cursor.close();
      }
}

你可能感兴趣的:(java,android,uri)