安卓的联系人,短信和通话记录能够经过一个叫作ContentResolver的类来读取 而获取ContentResolver是经过Context获取的正则表达式
ContentResolver cr = getApplicationContext().getContentResolver();
复制代码
获取联系人json
//contactsNum表明数量
public static String getAllContact(ContentResolver cr, int contactsNum) {
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
JSONArray jsonarray = new JSONArray();//json数组
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
if (cursor.getPosition() > contactsNum - 1) {
continue;
}
// 取得联系人的名字索引
int nameIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameIndex);
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
JSONObject jsonObj = new JSONObject();//对象,json形式
phone = phone.replace(" ", "");
if (!TextUtils.isEmpty(contact) && !TextUtils.isEmpty(phone)) {
try {
jsonObj.put("name", contact);//向pet对象里面添加值
jsonObj.put("phone", phone);
// 把每一个数据看成一对象添加到数组里
jsonarray.put(jsonObj);//向json数组里面添加pet对象
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
cursor.close();
}
if (jsonarray.length() <= 0) {
ToastUtils.showToast("请检查联系人");
}
return jsonarray.toString();
}
复制代码
获取短信数组
public static String getSmsInPhone(ContentResolver cr, int smsNum) {
final String SMS_URI_ALL = "content://sms/";
JSONArray jsonarray = new JSONArray();//json数组
try {
Uri uri = Uri.parse(SMS_URI_ALL);
String[] projection = new String[]{"_id", "address", "person",
"body", "date", "type"};
Cursor cur = cr.query(uri, projection, null,
null, "date desc"); // 获取手机内部短信
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
if (cur.getPosition() > smsNum - 1) {
continue;
}
JSONObject jsonObj = new JSONObject();//对象,json形式
String strAddress = cur.getString(cur.getColumnIndex("address"));
String strPerson = cur.getString(cur.getColumnIndex("person"));
String strbody = cur.getString(cur.getColumnIndex("body"));
long longDate = cur.getLong(cur.getColumnIndex("date"));
int intType = cur.getInt(cur.getColumnIndex("type"));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(longDate);
String strDate = dateFormat.format(d);
String strType = "";
if (intType == 1) {
strType = "in";
} else if (intType == 2) {
strType = "out";
} else {
strType = "null";
}
jsonObj.put("name", strPerson);//向pet对象里面添加值
jsonObj.put("phone", strAddress);
jsonObj.put("direction", strType);
jsonObj.put("sendTime", strDate);
jsonObj.put("content", strbody);
jsonarray.put(jsonObj);
}
cur.close();
}
} catch (SQLiteException ex) {
ex.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonarray.toString();
}
复制代码
获取通话记录bash
public static String getCallsInPhone(ContentResolver cr, int callRecordNum) {
Cursor cursor = cr.query(
CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls.DURATION, CallLog.Calls.TYPE, CallLog.Calls.DATE,
CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME}, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
JSONArray jsonarray = new JSONArray();//json数组
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
if (cursor.getPosition() > callRecordNum - 1) {
continue;
}
JSONObject jsonObj = new JSONObject();//对象,json形式
int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
long duration = cursor.getLong(cursor
.getColumnIndex(CallLog.Calls.DURATION));
String strPhone = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(cursor.getString(cursor
.getColumnIndex(CallLog.Calls.DATE))));
String date = dateFormat.format(d);
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
String strType = "";
switch (type) {
case CallLog.Calls.INCOMING_TYPE:
strType = "in";
break;
case CallLog.Calls.OUTGOING_TYPE:
strType = "out";
break;
case CallLog.Calls.MISSED_TYPE:
strType = "in";
break;
default:
break;
}
try {
if (TextUtils.isEmpty(name)) {
jsonObj.put("name", "");
} else {
jsonObj.put("name", name);
}
jsonObj.put("phone", strPhone);
jsonObj.put("direction", strType);
jsonObj.put("duration", duration);
jsonObj.put("callTime", date);
// 把每一个数据看成一对象添加到数组里
jsonarray.put(jsonObj);//向json数组里面添加pet对象
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return jsonarray.toString();
}
复制代码
获取验证码 咱们能够经过ContentObserver来获取相应的短信信息,可是要注意一点就是要对ContentObserver进行注销。ide
public class MessageContentObserver extends ContentObserver {
public final String SMS_URI_INBOX = "content://sms/inbox";//收信箱
private String smsContent = "";//验证码
private EditText verifyText = null;//验证码编辑框
private String smsAddress = null;//短信发送提供商
private String smsID = "";
//短信观察者 收到一条短信时 onchange方法会执行两次,因此比较短信id,若是一致则不处理
public MessageContentObserver(Handler handler, EditText verifyText) {
super(handler);
this.verifyText = verifyText;
}
public MessageContentObserver(Handler handler, EditText verifyText, String smsAddressNumber) {
super(handler);
this.verifyText = verifyText;
this.smsAddress = smsAddressNumber;
}
@NeedPermission(PermissionStringUtils.PERMISSION_SMS)
@Override
public void onChange(boolean selfChange) {
Cursor cursor = null;// 光标
// 读取收件箱中指定号码的短信
if (!TextUtils.isEmpty(smsAddress)) {
cursor = AppUtils.getContext().getContentResolver().query(Uri.parse(SMS_URI_INBOX),
new String[]{"_id", "address", "body", "read"}, //要读取的属性
null, //查询条件是什么
new String[]{smsAddress, "0"},//查询条件赋值
"date desc");//排序
} else {
cursor = AppUtils.getContext().getContentResolver().query(Uri.parse(SMS_URI_INBOX),
new String[]{"_id", "address", "body", "read"}, //要读取的属性
null, //查询条件是什么
null,//查询条件赋值
"date desc");//排序
}
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
//比较和上次接收到短信的ID是否相等
if (!smsID.equals(cursor.getString(cursor.getColumnIndex("_id"))) && cursor.getString(cursor.getColumnIndex("body")).contains("验证码")) {
String smsbody = cursor.getString(cursor.getColumnIndex("body"));
//用正则表达式匹配验证码 不肯定几位
Pattern pattern = Pattern.compile("[0-9]{4,9}");
Matcher matcher = pattern.matcher(smsbody);
if (matcher.find()) {//匹配到6位的验证码
smsContent = matcher.group();
if (verifyText != null && !TextUtils.isEmpty(smsContent)) {
verifyText.requestFocus();//获取焦点
verifyText.setText(smsContent);//设置文本
verifyText.setSelection(smsContent.length());//设置光标位置
}
}
smsID = cursor.getString(cursor.getColumnIndex("_id"));
}
}
cursor.close();
}
}
public void onDestory() {
if (verifyText != null) {
verifyText = null;
}
if (!TextUtils.isEmpty(smsAddress))
smsAddress = null;
}
}
复制代码
注销ContentObserverui
public void unRegister() {
if (messageContentObserver != null) {
messageContentObserver.onDestory();
AppUtils.getContext().getContentResolver().unregisterContentObserver(messageContentObserver);
messageContentObserver = null;
}
}
复制代码