Android SMS 簡訊讀取

來源:互聯網
上載者:User

Android SMS Read

package com.homer.sms;</p><p>import java.sql.Date;<br />import java.text.SimpleDateFormat;</p><p>import android.app.Activity;<br />import android.database.Cursor;<br />import android.database.sqlite.SQLiteException;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.util.Log;<br />import android.widget.ScrollView;<br />import android.widget.TableLayout;<br />import android.widget.TextView;</p><p>/**<br /> * 讀取手機簡訊<br /> *<br /> * @author sunboy_2050<br /> * @since http://blog.csdn.net/sunboy_2050<br /> * @date 2012.03.06<br /> */<br />public class smsRead extends Activity {</p><p>@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);</p><p>TextView tv = new TextView(this);<br />tv.setText(getSmsInPhone());</p><p>ScrollView sv = new ScrollView(this);<br />sv.addView(tv);</p><p>setContentView(sv);<br />}</p><p>public String getSmsInPhone() {<br />final String SMS_URI_ALL = "content://sms/";<br />final String SMS_URI_INBOX = "content://sms/inbox";<br />final String SMS_URI_SEND = "content://sms/sent";<br />final String SMS_URI_DRAFT = "content://sms/draft";<br />final String SMS_URI_OUTBOX = "content://sms/outbox";<br />final String SMS_URI_FAILED = "content://sms/failed";<br />final String SMS_URI_QUEUED = "content://sms/queued";</p><p>StringBuilder smsBuilder = new StringBuilder();</p><p>try {<br />Uri uri = Uri.parse(SMS_URI_ALL);<br />String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };<br />Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");// 擷取手機內部簡訊</p><p>if (cur.moveToFirst()) {<br />int index_Address = cur.getColumnIndex("address");<br />int index_Person = cur.getColumnIndex("person");<br />int index_Body = cur.getColumnIndex("body");<br />int index_Date = cur.getColumnIndex("date");<br />int index_Type = cur.getColumnIndex("type");</p><p>do {<br />String strAddress = cur.getString(index_Address);<br />int intPerson = cur.getInt(index_Person);<br />String strbody = cur.getString(index_Body);<br />long longDate = cur.getLong(index_Date);<br />int intType = cur.getInt(index_Type);</p><p>SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");<br />Date d = new Date(longDate);<br />String strDate = dateFormat.format(d);</p><p>String strType = "";<br />if (intType == 1) {<br />strType = "接收";<br />} else if (intType == 2) {<br />strType = "發送";<br />} else {<br />strType = "null";<br />}</p><p>smsBuilder.append("[ ");<br />smsBuilder.append(strAddress + ", ");<br />smsBuilder.append(intPerson + ", ");<br />smsBuilder.append(strbody + ", ");<br />smsBuilder.append(strDate + ", ");<br />smsBuilder.append(strType);<br />smsBuilder.append(" ]\n\n");<br />} while (cur.moveToNext());</p><p>if (!cur.isClosed()) {<br />cur.close();<br />cur = null;<br />}<br />} else {<br />smsBuilder.append("no result!");<br />} // end if</p><p>smsBuilder.append("getSmsInPhone has executed!");</p><p>} catch (SQLiteException ex) {<br />Log.d("SQLiteException in getSmsInPhone", ex.getMessage());<br />}</p><p>return smsBuilder.toString();<br />}<br />}

AndroidManifest.xml 許可權

 記得在AndroidManifest.xml中加入android.permission.READ_SMS這個permission

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


運行結果:

程式碼範例


URI主要有:

content://sms/               所有簡訊
content://sms/inbox        收件匣
content://sms/sent          已發送
content://sms/draft         草稿
content://sms/outbox     寄件匣
content://sms/failed       發送失敗
content://sms/queued    待發送列表

sms主要結構: 

  1. _id => 短訊息序號 如100  
  2. thread_id => 對話的序號 如100  
  3. address => 寄件者地址,手機號.如+8613811810000  
  4. person => 寄件者,返回一個數字就是連絡人清單裡的序號,陌生人為null  
  5. date => 日期  long型。如1256539465022  
  6. protocol => 協議 0 SMS_RPOTO, 1 MMS_PROTO   
  7. read => 是否閱讀 0未讀, 1已讀   
  8. status => 狀態 -1接收,0 complete, 64 pending, 128 failed   
  9. type => 類型 1是接收到的,2是已發出   
  10. body => 短訊息內容   
  11. service_center => 簡訊服務中心號碼編號。如+8613800755500  

String[] projection = new String[]{"address", "body"};
Cursor cursor = getContentResolver().query(uri, projection, "where .." new String[]{"", ""}, "order by ..")


Android簡訊儲存資料庫

偶然發現了Android源碼中的一個類MmsSmsDatabaseHelper.java,原來android將所有的簡訊資訊都存入了mmssms.db中。

公開的SDK中沒有這個類,不能直接使用。於是自己寫了一個SQLiteOpenHelper,但是查詢的時候發生SQL異常。看來不能為所欲為了,不過據網上資料介紹可以拷貝db檔案來實現簡訊資料備份。

MmsSmsDatabaseHelper.java在Android源碼中的路徑:

packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java


sms資料庫中的欄位如下:

_id               一個自增欄位,從1開始
thread_id    序號,同一發信人的id相同
address      寄件者手機號碼
person        連絡人清單裡的序號,陌生人為null 
date            發件日期
protocol      協議,分為: 0 SMS_RPOTO, 1 MMS_PROTO  
read           是否閱讀 0未讀, 1已讀  
status         狀態 -1接收,0 complete, 64 pending, 128 failed 
type     
    ALL    = 0;
    INBOX  = 1;
    SENT   = 2;
    DRAFT  = 3;
    OUTBOX = 4;
    FAILED = 5;
    QUEUED = 6;
 
body                     簡訊內容
service_center     簡訊服務中心號碼編號
subject                  簡訊的主題
reply_path_present     TP-Reply-Path
locked    


sms資料庫表欄位類型的源碼:

private void createSmsTables(SQLiteDatabase db) {<br /> // N.B.: Whenever the columns here are changed, the columns in<br /> // {@ref MmsSmsProvider} must be changed to match.<br /> db.execSQL("CREATE TABLE sms (" +<br /> "_id INTEGER PRIMARY KEY," +<br /> "thread_id INTEGER," +<br /> "address TEXT," +<br /> "person INTEGER," +<br /> "date INTEGER," +<br /> "date_sent INTEGER DEFAULT 0," +<br /> "protocol INTEGER," +<br /> "read INTEGER DEFAULT 0," +<br /> "status INTEGER DEFAULT -1," + // a TP-Status value<br /> // or -1 if it<br /> // status hasn't<br /> // been received<br /> "type INTEGER," +<br /> "reply_path_present INTEGER," +<br /> "subject TEXT," +<br /> "body TEXT," +<br /> "service_center TEXT," +<br /> "locked INTEGER DEFAULT 0," +<br /> "error_code INTEGER DEFAULT 0," +<br /> "seen INTEGER DEFAULT 0" +<br /> ");");<br />....<br />}

packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java


連絡人為空白

簡訊資料庫裡面如果你是先受到陌生簡訊之後再把陌生人新增至連絡人清單的話,簡訊資料庫裡面的person欄位就為null,如果你是先新增連絡人...再發簡訊的話,簡訊資料庫裡面的person欄位就不為空白了,所以你要是想通過簡訊資料庫裡的欄位取得連絡人的其他資訊的話,只能通過地址來取。

參考推薦:

Android SMS Messaging 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.