本文共 2416 字,大约阅读时间需要 8 分钟。
读取短信需要的权限
读取数据库短信方法
public static List > getSmsCode() { String lastTime = "1534228493681"; // 时间 Log.i("SMSUtil", "开始获取短信"); Cursor cursor = null; // 添加异常捕捉 try { //第一种, 查询所有短信 cursor = App.mContext.getContentResolver().query( Uri.parse("content://sms"), new String[]{"_id", "address", "body", "date", "person", "type"}, null, null, "date desc"); //第二种, 通过查询条件, 例如:date > lastTime, 过滤数据 /*cursor = App.mContext.getContentResolver().query( Uri.parse("content://sms"), new String[]{"_id", "address", "body", "date", "person", "type"}, "date > ?", new String[]{lastTime}, "date desc");*/ if (cursor != null) { List > smsList = new ArrayList<>(); while (cursor.moveToNext()) { String body = cursor.getString(cursor.getColumnIndex("body"));// 在这里获取短信信息 String person = cursor.getString(cursor.getColumnIndex("person")); // 陌生人为null String address = cursor.getString(cursor.getColumnIndex("address")); String _id = cursor.getString(cursor.getColumnIndex("_id")); String date = cursor.getString(cursor.getColumnIndex("date")); String type = cursor.getString(cursor.getColumnIndex("type")); HashMap smsMap = new HashMap<>(); smsMap.put("body", body); smsMap.put("person", person); smsMap.put("address", address); smsMap.put("_id", _id); smsMap.put("date", date); smsList.add(smsMap); Log.i("test_sms", "body = " + body + " person = " + person + " address = " + address + " date = " + date + " type = " + type); } // 返回所有的短信 return smsList; } } catch (Exception e) { e.printStackTrace(); Log.i("test_sms", "e = " + e.getMessage()); } finally { if (cursor != null) { cursor.close(); } } return null; }
URI 主要有:
转载于:https://my.oschina.net/u/3611008/blog/2254649