package MyGetContacts.code;
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 {
/** Called when the activity is first created. */
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textView);
String str = getQueryData();
textView.setText(str);
}
private String getQueryData()
{
String result = "";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
int idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
//for(cursor.moveToFirst();(!cursor.isAfterLast());cursor.moveToNext())
while(cursor.moveToNext())
{
String contactId = cursor.getString(idIndex);
Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null,null);
result = result + contactId + "\t";
while(phone.moveToNext())
{
int i = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String num = phone.getString(i);
result += num;
}
result = result + cursor.getString(nameIndex) + "\t\n";
phone.close();
}
cursor.close();
return result;
}
}
最后别忘了 加上 <uses-permission android:name="android.permission.READ_CONTACTS"/> 这个权限,否则运行会出错
(真不知道google加这些权限干嘛用,脑残~~~)