数据库获取 Android 短信

本文首发地址:yangxiaoge

读取短信需要的权限

<uses-permission android:name="android.permission.READ_SMS"/>

读取数据库短信方法

   public static List<Map<String, String>> 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<Map<String, String>> 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<String, String> 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 主要有:

content://sms/             所有短信 (本示例用的所有)
content://sms/inbox        收件箱
content://sms/sent         已发送
content://sms/draft        草稿
content://sms/outbox       发件箱
content://sms/failed       发送失败
content://sms/queued       待发送列表

SMS 主要结构:

_id => 短消息序号 如 100  
thread_id => 对话的序号 如 100  
address => 发件人地址,手机号. 如 + 8613811810000  
person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为 null  
date => 日期  long 型。如 1256539465022  
protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO   
read => 是否阅读 0 未读, 1 已读   
status => 状态 -1 接收,0 complete, 64 pending, 128 failed   
type => 类型 1 是接收到的,2 是已发出       
   (ALL    = 0; 所有
    INBOX  = 1; 收件箱
    SENT   = 2; 已发送
    DRAFT  = 3; 草稿
    OUTBOX = 4; 发件箱
    FAILED = 5; 失败
    QUEUED = 6;)待发送

body => 短消息内容   
service_center => 短信服务中心号码编号。如 + 8613800755500  

参考链接

https://blog.csdn.net/ithomer/article/details/7328321
https://blog.csdn.net/laichao1112/article/details/6436511

猜你喜欢

转载自blog.csdn.net/nn991926/article/details/81667452