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主要結構:
- _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是已發出
- body => 短訊息內容
- 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