1.联系人存储业务bean
ContactInfo.java
2.ContactInfoSer.java
读取系统联系人数据库contacts.db 查找需要表字段 raw_contacts,data;
返回List<ContactInfo> contactInfos = new ArrayList<ContactInfo>(); 找到的联系人数据集合
3.ListContact.java
1.接收返回的联系人list集合。
2.设置adapter 讲数据显示在 list item
lv_select_contact.setAdapter(new ContactAdapter());
private class ContactAdapter extends BaseAdapter{}
3.设置list item 监听完成单击时返回单击的item数据
lv_select_contact.setOnItemClickListener(new OnItemClickListener(){});
MainActivity.java-获取返回的值
. Intent intent = new Intent(this,ListContact.class);
startActivityForResult(intent, 0);//获取新activity返回的值
重载 onActivityResult() 当创建的activity关闭时,调用此方法;并把数据 Intent data 传递給调用它的activity
package com.example.SmsSelectContact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button selectBut,sendsmsBut;
private EditText contactText,smsText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
selectBut = (Button) findViewById(R.id.selectBut);
sendsmsBut =(Button) findViewById(R.id.sendBut);
smsText = (EditText) findViewById(R.id.smsText);
contactText = (EditText) findViewById(R.id.contactText);
}
//选择联系人按钮
public void select_but(View v){
//启动目标activity
Intent intent = new Intent(this,ListContact.class);
//开启新的activity
//startActivity(intent);
startActivityForResult(intent, 0);//获取新activity返回的值
}
//发送sms按钮
public void sendSMS(View v){
}
//当创建的activity关闭时,调用此方法;并把数据 Intent data 传递給调用它的activity
//
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//接收创建的activity 返回的值
if(data != null){
String number = data.getStringExtra("number");
contactText.setText(number);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
package com.example.SmsSelectContact;
import java.util.List;
import com.example.SmsSelectContact.domain.ContactInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListContact extends Activity {
private ListView lv_select_contact;
List<ContactInfo> contactInfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_contact);
lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);
//接收返回的 返回所有联系人信息 集合
contactInfos = ContactInfoSer.getContactInfos(this);
//2.adapter 适配器
lv_select_contact.setAdapter(new ContactAdapter());
//3.设置item条目单击监听
lv_select_contact.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
ContactInfo contactInfo = contactInfos.get(position);
String number = contactInfo.getNumber(); //获取点击item 号码
//把号码传递给调用它的activity
//通过第二个 intent data参数传递出去
Intent data = new Intent();
data.putExtra("number", number);
setResult(0,data);
//点击item 条目时就关闭掉当前activity,然后把数据传给调用者 执行OnActivityResult()方法
finish();
}
});
}
private class ContactAdapter extends BaseAdapter{
@Override
public int getCount() {
return contactInfos.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactInfo contactInfo = contactInfos.get(position);
View v = View.inflate(getApplicationContext(), R.layout.contact_item, null);
TextView tv_name = (TextView) v.findViewById(R.id.tv_item_name);
TextView tv_number = (TextView) v.findViewById(R.id.tv_item_number);
tv_name.setText(contactInfo.getName());
tv_number.setText(contactInfo.getNumber());
return v;
}
}
}
package com.example.SmsSelectContact;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.example.SmsSelectContact.domain.ContactInfo;
public class ContactInfoSer {
/**
* 读取系统联系人数据库
* 返回所有联系人信息
* @return List<ContactInfo>
*/
public static List<ContactInfo> getContactInfos(Context context){
//创建一个联系人集合
List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
// 1.查询raw_contact表获取联系人ID
ContentResolver resolver = context.getContentResolver();
// 2.获取raw_contact表对应的uri
Uri contactUri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri dataUri = Uri.parse("content://com.android.contacts/data");
Cursor contactCursor = resolver.query(contactUri, null, null, null,null);
while (contactCursor.moveToNext()) {
//联系人数据类bean
ContactInfo contactInfo = new ContactInfo();
// 获取联系人id:contact_id
String id = contactCursor.getString(contactCursor.getColumnIndex("contact_id"));
System.out.println("联系人:" + id);
if (id != null) {
// 通过id获取data表对应信息
Cursor dataCursor = resolver.query(
dataUri,
null,
"raw_contact_id=?",
new String[] { id },
null);
while (dataCursor.moveToNext()) {
// 查询 data 表对应的 view_data视图
String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));
String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
System.out.println("data: = " + data1);
System.out.println("mimetype_id = " + mimetype);
//添加数据
if("vnd.android.cursor.item/name".equals(mimetype)){
contactInfo.setName(data1);
}
if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
contactInfo.setNumber(data1);
}
}
contactInfos.add(contactInfo);
dataCursor.close();
System.out.println("--------------");
}
}
contactCursor.close();
return contactInfos;
}
}
package com.example.SmsSelectContact.domain;
/**
* 联系人业务bean
* @author Administrator
*
*/
public class ContactInfo {
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
http://pan.baidu.com/s/1i3Kdt0T 代码