首先这里介绍一下 获取思路。
第一种 : 直接查通讯录的数据库
第二种 :设用通讯录的Activity 并返回所选择的数据。
先实现第二种吧。、第二种相对简单,易操作、
你要操作 手机中的通讯录,就要有权限 的操作。
android 内核linux
1、要在工程中 AndroidManifest.xml 文件中添加操作权限
然后在你的activity 里面编写调用代码:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CONTACT);
重写 Activity 的 onActivityResult 方法:
//重写的结果返回方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (data == null) {
return;
}
Uri result = data.getData();
String contactId = result.getLastPathSegment();
String name = "";
String phone = "";
//得到名称
String[] projection = new String[] { People._ID, People.NAME ,People.NUMBER};
Cursor cursor = getContentResolver().query(People.CONTENT_URI,
projection, // select sentence
People._ID + " = ?", // where sentence
new String[] { contactId }, // where values
People.NAME); // order by
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(People.NAME));
}
//得到 电话
projection = new String[] { Contacts.Phones.PERSON_ID, Contacts.Phones.NUMBER};
cursor = getContentResolver().query(Contacts.Phones.CONTENT_URI,
projection, // select sentence
Contacts.Phones.PERSON_ID + " = ?", // where sentence
new String[] { contactId }, // where values
Contacts.Phones.NAME); // order by
if (cursor.moveToFirst()) {
phone = cursor.getString(cursor.getColumnIndex(Contacts.Phones.NUMBER));
}
//显示
showPhone.setText(name+":"+phone);
break;
}
}
就可以得到你选择的 联系的信息了。
下面来看一下我的例子和载图吧。 :
1、添加操作权限 AndroidManifest.xml
上面有载图自己看。
2、创建 layout 文件 get_phone_num.xml
一个button 一个view
3、创建Activity 类 GetPhoneNumActivity.java
package com.main;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.main.db.FriendsDao;
public class AddUserActivity extends Activity {
private EditText name;
private EditText phoneNum;
private EditText birthday;
private String sexText;
private FriendsDao friendsDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_user);
//得到页面控件
name=(EditText)findViewById(R.id.addUserTextName);
phoneNum=(EditText)findViewById(R.id.addUserTextNum);
birthday=(EditText)findViewById(R.id.addUserTextBirthday);
RadioGroup sex=(RadioGroup)findViewById(R.id.addUserTextSex);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("test", "select radio " + sexText);
sexText=((RadioButton)findViewById(checkedId)).getText().toString();
Log.d("test", "select radio " + sexText);
}
});
friendsDao=new FriendsDao(this,"ll1x.db",null,2);
}
public void addUserClick(View view){
int viewId=view.getId();
switch(viewId){
case R.id.addUserOkBut://确定
//验证
String name=this.name.getText().toString();
String phoneNum=this.phoneNum.getText().toString();
String birthday=this.birthday.getText().toString();
//提交 保存数据库
friendsDao.insertData(name, phoneNum,birthday,sexText);
Log.d("test", "addUserClick " + name+"|"+phoneNum+"|"+birthday+"|"+sexText.toString());
break;
case R.id.addUserNoBut://取消
break;
case R.id.addUserFromTXId:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
break;
}
}
//重写的结果返回方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (data == null) {
return;
}
Uri result = data.getData();
String contactId = result.getLastPathSegment();
String name = "";
String phone = "";
//得到名称
String[] projection = new String[] { People._ID, People.NAME ,People.NUMBER};
Cursor cursor = getContentResolver().query(People.CONTENT_URI,
projection, // select sentence
People._ID + " = ?", // where sentence
new String[] { contactId }, // where values
People.NAME); // order by
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(People.NAME));
}
//得到 电话
projection = new String[] { Contacts.Phones.PERSON_ID, Contacts.Phones.NUMBER};
cursor = getContentResolver().query(Contacts.Phones.CONTENT_URI,
projection, // select sentence
Contacts.Phones.PERSON_ID + " = ?", // where sentence
new String[] { contactId }, // where values
Contacts.Phones.NAME); // order by
if (cursor.moveToFirst()) {
phone = cursor.getString(cursor.getColumnIndex(Contacts.Phones.NUMBER));
}
//显示
showPhone.setText(name+":"+phone);
break;
}
}
}
运行载图:
资料:
http://hi.baidu.com/%C1%F5%D0%A1%C0%F61984519/blog/item/d58cc37b9aa1daf32e73b3ae.html
http://blog.sina.com.cn/s/blog_60a94ae70100vfrm.html
就到这里,不懂Call 我。
ok 就到这里、
做了一下修改:
Phone 改成了 PhoneLookup