天天看點

資料庫擷取 Android 短信

讀取短信需要的權限
<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開發中文站

”,了解相關資訊可以關注“

”。