您现在的位置是:首页 > 文章详情

数据库获取 Android 短信

日期:2018-08-15点击:330
读取短信需要的权限
<uses-permission android:name="android.permission.READ_SMS"/>
读取数据库短信方法
   public static List<Map<StringString>> 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"},
                    nullnull"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<StringString>> 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<StringString> 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  


原文发布时间为:2018-08-15

本文来自云栖社区合作伙伴“Android开发中文站”,了解相关信息可以关注“Android开发中文站”。

原文链接:https://yq.aliyun.com/articles/625889
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章