package com.yancms.lianxi;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Two extends Activity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
button=(Button)findViewById(R.id.btn2);
textView=(TextView) findViewById(R.id.tv2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String string ="";
//得到 ContentResolver对象
ContentResolver cr=getContentResolver();
//取得电话本中开始一行的光标
Cursor cursor=cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动光标
while (cursor.moveToNext()) {
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
string += contact + ":";
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
while (phone.moveToNext())
{
int numberFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String strPhoneNumber = phone.getString(numberFieldColumnIndex);
string += " " + strPhoneNumber;
}
string += "\n";
phone.close();
/*//取得联系人名字 android2.0之前使用的方法
int nameFieldColumnIndex=cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact=cursor.getString(nameFieldColumnIndex);
System.out.print(contact);
//取得电话号码
int numberFieldColumnIndex=cursor.getColumnIndex(PhoneLookup.NUMBER);
String number =cursor.getString(numberFieldColumnIndex);
System.out.print(number);
string +=(contact+":"+number+"\n"); */
}
cursor.close();
//设置TextView显示的内容
textView.setText(string);
//显示到屏幕
//setContentView(textView);
}
});
}
}