数据库获取 Android 短信

170 阅读6分钟
原文链接: click.aliyun.com

数据库获取 Android 短信

技术小能手 2018-08-16 14:50:27 浏览320 评论0
  • 云栖社区
  • android
  • LOG
  • 数据库
  • string
  • PUT
  • type
  • list
  • hashmap

摘要: 读取短信需要的权限 <uses-permission android:name="android.permission.READ_SMS"/> 读取数据库短信方法    public static List<Map<String, String>> getSmsCode() {         S...

读取短信需要的权限
<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  


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

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

用云栖社区APP,舒服~

【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行,现场仅600席,免费赠票领取入口  详情请点击
分享到:

相关文章

网友评论