Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

讀取 Android 手機中所有簡訊:ContentResolver 實作與欄位說明

編輯:Android 開發實例

本文示範如何透過 ContentResolver 查詢裝置中的簡訊資料庫,把收件箱、發件箱、草稿等全部訊息讀取出來並整理成字串。

 public String getSmsInPhone() {
   final String SMS_URI_ALL = "content://sms/";
   final String SMS_URI_INBOX = "content://sms/inbox";
   final String SMS_URI_SEND = "content://sms/sent";
   final String SMS_URI_DRAFT = "content://sms/draft";

   StringBuilder smsBuilder = new StringBuilder();
   try{
     ContentResolver cr = getContentResolver();
     String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
     Uri uri = Uri.parse(SMS_URI_ALL);
     Cursor cur = cr.query(uri, projection, null, null, "date desc");

     if (cur.moveToFirst()) {
       String name;
       String phoneNumber;
       String smsbody;
       String date;
       String type;
       int nameColumn = cur.getColumnIndex("person");
       int phoneNumberColumn = cur.getColumnIndex("address");
       int smsbodyColumn = cur.getColumnIndex("body");
       int dateColumn = cur.getColumnIndex("date");
       int typeColumn = cur.getColumnIndex("type");

       do{
         name = cur.getString(nameColumn);
         phoneNumber = cur.getString(phoneNumberColumn);
         smsbody = cur.getString(smsbodyColumn);
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
         Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
         date = dateFormat.format(d);

         int typeId = cur.getInt(typeColumn);
         if(typeId == 1){
           type = "接收";
         } else if(typeId == 2){
           type = "發送";
         } else {
           type = "";
         }

         smsBuilder.append("[");
         smsBuilder.append(name + ",");
         smsBuilder.append(phoneNumber + ",");
         smsBuilder.append(smsbody + ",");
         smsBuilder.append(date + ",");
         smsBuilder.append(type);
         smsBuilder.append("] ");
         if(smsbody == null) smsbody = "";
       }while(cur.moveToNext());
     } else {
       smsBuilder.append("no result!");
     }
     smsBuilder.append("getSmsInPhone has executed!");
   } catch(SQLiteException ex) {
     Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
   }
   return smsBuilder.toString();
 }

程式碼要點

1、這個函式取得手機中所有簡訊,涵蓋收件箱、發件箱、草稿箱等。若只想取特定信箱,把 URI 換成 SMS_URI_INBOX、SMS_URI_SEND、SMS_URI_DRAFT 即可。

2、因為沒有使用 Activity 相關的 API,這個函式可以放在 Service 子類中執行。

3、取得的欄位包含:寄件人姓名、手機號碼、簡訊內容、時間、訊息類型。

4、需先取得讀取權限,否則查詢會失敗。

sms 表的主要欄位

_id:簡訊序號,如 100

thread_id:對話序號。與同一號碼互傳的簡訊共享同一個 thread_id

address:寄件人手機號碼,如 +8613811810000

person:寄件人姓名。若對方在聯絡人中則為姓名,陌生號碼則為 null

date:日期,long 型別,如 1256539465022(毫秒時間戳)

protocol:0 為 SMS_PROTO 簡訊,1 為 MMS_PROTO 多媒體簡訊

read:是否已讀,0 未讀、1 已讀

status:簡訊狀態,-1 接收中、0 complete、64 pending、128 failed

type:訊息類型,1 為已接收、2 為已發送

body:簡訊內容

service_center:簡訊服務中心號碼,如 +8613800755500

權限設定與實作建議

在 AndroidManifest.xml 中宣告權限:

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

這裡必須提醒幾個實務上的重點,它們比程式碼本身更關鍵。

權限已非安裝即取得。READ_SMS 屬於高風險權限,Android 4.4 之後讀取簡訊還需要 READ_SMS 或成為預設簡訊應用;Android 6.0 起更必須在執行期動態請求。更嚴格的是,Google Play 對 READ_SMS 與 CALL_LOG 這類權限設有政策限制——除非你的應用屬於「核心功能就是處理簡訊」的類別(預設簡訊 App、行動電話管理、備份還原等),否則商店審核會直接駁回。一般應用若只是想做「註冊時自動讀取驗證碼」,建議改用 SMS Retriever API 或 One-time Password 服務,無需敏感權限。

用 ContentResolver 的正確姿势。查詢後記得關閉 Cursor(try-with-resources 或 onDestroy 中 close),否則會洩漏;查詢本身屬於 I/O 操作,應放在子執行緒;date 欄位是毫秒時間戳,範例中的 SimpleDateFormat 建議加上 Locale.getDefault() 參數,避免在不同語言環境下出現解析異常。

程式碼中的一個小瑕疵。範例在 smsbody == null 的判斷放在 append 之後,實際上沒有發揮作用;若真要防 null,應在 append 之前處理。此外 person 欄位在新版系統上常為 null,可改以 ContactsContract 反查名稱。

以上就是讀取簡訊的完整做法與注意事項,希望對大家的 Android 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved