网上获取短信内容,通话记录以及通信录功能都是通过ContentResolver来获取,本文稍微进行整理一下,真机小米Note4X测试通过,编译平台Android studio。
信息获取流程基本一致,三步:
一、获取短信内容
1、获取ContentResolver:
ContentResolver resolver = getContentResolver();
2、传入参数及说明:
Cursor cursor = resolver.query(Telephony.Sms.CONTENT_URI, new String[]{
Telephony.Sms.ADDRESS, Telephony.Sms.BODY, Telephony.Sms.DATE, Telephony.Sms.READ, Telephony.Sms.STATUS,
Telephony.Sms.TYPE}, null, null, "date DESC");
参数Telephony.Sms.CONTENT_URI为短信uri,DEFAULT_SORT_ORDER = date DESC按时间来进行排序记录,如果只想要最后一条记录,需改为date DESC limit 1。
短信属性说明:
Telephony.Sms.ADDRESS 发件人地址,即手机号
Telephony.Sms.BODY 短信具体内容
Telephony.Sms.DATE 日期,long型
Telephony.Sms.READ 是否阅读0未读,1已读
Telephony.Sms.STATUS 短信状态-1 接收,0 complete,64 pending,128 failed
Telephony.Sms.TYPE 短信类型1是接收到的,2是已发出
3、获取数据
0-6条数据对应参数中的顺序,需要一一对应。
如cursor.getString(0)为Telephony.Sms.ADDRESS 发件人地址,即手机号;
如cursor.getString(1)为Telephony.Sms.BODY 短信具体内容;
"号码:" + cursor.getString(0) + "\n"
"姓名:" + getPerson(cursor.getString(0)) + "\n"
"状态:" + getMessageRead(cursor.getInt(3)) + "\n"
"类型:" + getMessageType(cursor.getInt(5)) + "\n"
"内容:" + cursor.getString(1) + "\n"
"日期:" + formatDate(cursor.getLong(2)) + "\n\n";
4、获取短信代码如下:
private void getSmsInPhone() {
String SmsInfo = "";
//动态申请权限
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS}, 101);
return;
}
//动态申请权限
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_SMS}, 101);
return;
}
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Telephony.Sms.CONTENT_URI, new String[]{
Telephony.Sms.ADDRESS, Telephony.Sms.BODY, Telephony.Sms.DATE, Telephony.Sms.READ, Telephony.Sms.STATUS,
Telephony.Sms.TYPE}, null, null, "date DESC");
if (cursor != null) {
while (cursor.moveToNext()) {
SmsInfo = SmsInfo + "号码:" + cursor.getString(0) + "\n"
+ "姓名:" + getPerson(cursor.getString(0)) + "\n"
+ "状态:" + getMessageRead(cursor.getInt(3)) + "\n"
+ "类型:" + getMessageType(cursor.getInt(5)) + "\n"
+ "内容:" + cursor.getString(1) + "\n"
+ "日期:" + formatDate(cursor.getLong(2)) + "\n\n";
}
cursor.close();
}
textView.setText(SmsInfo);
}
二、获取通话记录
和获取短信基本一样,主要是参数不一样:
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls.CACHED_FORMATTED_NUMBER, CallLog.Calls.CACHED_MATCHED_NUMBER,
CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.DURATION,
CallLog.Calls.GEOCODED_LOCATION}, null, null, "date DESC");
CallLog.Calls.CACHED_FORMATTED_NUMBER 通话记录格式化号码
CallLog.Calls.CACHED_MATCHED_NUMBER 通话记录为格式化号码
CallLog.Calls.CACHED_NAME 联系人名称
CallLog.Calls.TYPE 通话类型
CallLog.Calls.DATE 通话时间(long型)
CallLog.Calls.DURATION 通话时长(秒为单位)
CallLog.Calls.GEOCODED_LOCATION 运营商地址(如:浙江杭州)
通话类型
CallLog.Calls.INCOMING_TYPE 呼入
CallLog.Calls.OUTGOING_TYPE 呼出
CallLog.Calls.MISSED_TYPE 未接
代码如下:
private void GetCallsInPhone() {
String CallInfo = "";
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, 100);
return;
}
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls.CACHED_FORMATTED_NUMBER, CallLog.Calls.CACHED_MATCHED_NUMBER,
CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.DURATION,
CallLog.Calls.GEOCODED_LOCATION}, null, null, "date DESC");
if (cursor != null) {
try {
while (cursor.moveToNext()) {
CallInfo = CallInfo + "号码:" + cursor.getString(1) + "\n"
+ "姓名:" + cursor.getString(2) + "\n"
+ "类型:" + getCallType(cursor.getInt(3)) + "\n"
+ "日期:" + formatDate(cursor.getLong(4)) + "\n"
+ "时间:" + formatDuration(cursor.getLong(5)) + "\n\n";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close(); //关闭cursor,避免内存泄露
}
}
textView.setText(CallInfo);
}
三、获取通信录
查询参数:
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
代码如下:
private void getContact() {
String contactInfo = "";
ContentResolver resolver = getApplicationContext().getContentResolver();
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
int nameIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameIndex);
contactInfo += "姓名:" + contact + "\n";
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phone.moveToNext()) {//检查是否存在多个号码
String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactInfo = contactInfo + "号码:" + strPhoneNumber + "\n";
}
contactInfo += "\n\n";
phone.close();
}
cursor.close();
textView.setText(contactInfo);
}
源码Demo下载
原文地址:http://www.ctrlqun.com/index.php/blog/6.html