如何访问短信和联系人数据?
Posted
技术标签:
【中文标题】如何访问短信和联系人数据?【英文标题】:How to access SMS and contacts data? 【发布时间】:2011-11-04 11:09:46 【问题描述】:内容提供商是读取/写入诸如 SMS 和联系人等私人数据的唯一方法吗?我首先尝试了简单而懒惰的方式(复制短信和联系人 SQLite 数据库文件),但我遇到了一些权限问题。我之所以这么问,是因为我正在尝试备份和恢复 SMS 和联系人,而且要逐个访问数据字段需要做很多工作。
【问题讨论】:
【参考方案1】:获取联系人:
How to call android contacts list?
How to get contacts from native phonebook in android
How to obtain all details of a contact in Android
How to get the first name and last name from Android contacts?
How to import contacts from phonebook to our application
Android contacts extraction
How to get all android contacts but without those which are on SIM
对于短信:
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
new String[] "_id", "thread_id", "address", "person", "date",
"body", "type" , null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] "address", "person", "date", "body","type" ;
if (cursor1.getCount() > 0)
String count = Integer.toString(cursor1.getCount());
Log.e("Count",count);
while (cursor1.moveToNext())
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
【讨论】:
【参考方案2】:获取联系方式
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
while (cursor.moveToNext())
try
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext())
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
phones.close();
catch(Exception e)
从收件箱中读取短信
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor1 = getContentResolver().query(
mSmsinboxQueryUri,
new String[] "_id", "thread_id", "address", "person", "date",
"body", "type" , null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] "address", "person", "date", "body",
"type" ;
if (cursor1.getCount() > 0)
String count = Integer.toString(cursor1.getCount());
Log.e("Count",count);
while (cursor1.moveToNext())
out.write("<message>");
String address = cursor1.getString(cursor1
.getColumnIndex(columns[0]));
String name = cursor1.getString(cursor1
.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1
.getColumnIndex(columns[2]));
String msg = cursor1.getString(cursor1
.getColumnIndex(columns[3]));
String type = cursor1.getString(cursor1
.getColumnIndex(columns[4]));
【讨论】:
以上是关于如何访问短信和联系人数据?的主要内容,如果未能解决你的问题,请参考以下文章