您的位置:首页 > 移动开发 > Android开发

Android -- Contentprovider---listview 实现调用通讯录和短信接收

2015-09-05 09:40 621 查看


获取权限

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"/><!-- 读取通讯录权限 -->
<uses-permission android:name="android.permission.WRITE_CONTACTS"/><!-- 写入通讯录权限 -->


注册

<activity
android:name=".MessageActivity">

</activity>
<activity
android:name=".NumberActivity"
>

</activity>


NumberActivty

public class NumberActivity extends Activity {
ListView m_lstView; // 列表
private static final String NAME = "name";
private static final String NUMBER = "number";
List<HashMap<String,Object>> data;
HashMap<String,Object>map;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_listview);
m_lstView = (ListView)findViewById(R.id.listViewNumber);
data = new ArrayList<HashMap<String,Object>>();
getContacts();
ListAdapter adapter = new SimpleAdapter(this,
data,
android.R.layout.simple_expandable_list_item_2,
new String[]{"name","phone"},
new int[]{android.R.id.text1,android.R.id.text2}
);
m_lstView.setAdapter(adapter);
}
private void getContacts() {
// 得到ContentResolver对象
ContentResolver cr = this.getContentResolver();
// 取得电话本中开始一项的光标,主要就是查询"contacts"表
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
map = new HashMap<String,Object>();
// 取得联系人名字 (显示出来的名字),实际内容在 ContactsContract.Contacts中
int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = cursor.getString(nameIndex);

map.put("name",name);

// 取得联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

// 根据联系人ID查询对应的电话号码
Cursor phoneNumbers = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
// 取得电话号码(可能存在多个号码)
while (phoneNumbers.moveToNext())
{
String strPhoneNumber = phoneNumbers.getString(phoneNumbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

map.put("phone", strPhoneNumber);
}
phoneNumbers.close();

// 根据联系人ID查询对应的email
Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
+ contactId, null, null);
// 取得email(可能存在多个email)
while (emails.moveToNext())
{
String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
data.add(map);
}
cursor.close();
}
}


MessageActicty

public class MessageActivity extends Activity {
final String SMS_URI_ALL = "content://sms/draft";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_message);
fillListView();

}
private void fillListView() {
ListView view = (ListView) findViewById(R.id.sms_list);
ArrayList<HashMap<String, String>> list = readAllSMS();
SimpleAdapter listItemAdapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2,
new String[] {"addr","body"},
new int[] {android.R.id.text1, android.R.id.text2}
);
view.setAdapter(listItemAdapter);
}

private ArrayList<HashMap<String, String>> readAllSMS() {

ContentResolver cr = getContentResolver();
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_ALL);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>> ();
try{
Cursor cursor = cr.query(uri, projection, null, null, "date desc");

if(cursor.moveToFirst()) {
int addrIdx = cursor.getColumnIndex("address");
int personIdx = cursor.getColumnIndex("person");
int bodyIdx = cursor.getColumnIndex("body");
do {
String addr = cursor.getString(addrIdx);
String person = cursor.getString(personIdx);
String body = cursor.getString(bodyIdx);
HashMap<String, String> item = new HashMap<String, String>();
item.put("addr", addr);
item.put("person", person);
item.put("body", body);
list.add(item);
Log.i("xiaoxiong", person);
} while(cursor.moveToNext());
}
return list;
}catch (Exception ex) {
Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
}
return list;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: