Android SMS(一) —— 讀取簡訊
Android SMS Read
package com.homer.sms; import java.sql.Date; import java.text.SimpleDateFormat; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteException; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.ScrollView; import android.widget.TableLayout; import android.widget.TextView; /** * 讀取手機簡訊 * * @author sunboy_2050 * @since http://blog.csdn.net/sunboy_2050 * @date 2012.03.06 */ public class smsRead extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText(getSmsInPhone()); ScrollView sv = new ScrollView(this); sv.addView(tv); setContentView(sv); } 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"; final String SMS_URI_OUTBOX = "content://sms/outbox"; final String SMS_URI_FAILED = "content://sms/failed"; final String SMS_URI_QUEUED = "content://sms/queued"; StringBuilder smsBuilder = new StringBuilder(); try { Uri uri = Uri.parse(SMS_URI_ALL); String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc"); // 獲取手機內部簡訊 if (cur.moveToFirst()) { int index_Address = cur.getColumnIndex("address"); int index_Person = cur.getColumnIndex("person"); int index_Body = cur.getColumnIndex("body"); int index_Date = cur.getColumnIndex("date"); int index_Type = cur.getColumnIndex("type"); do { String strAddress = cur.getString(index_Address); int intPerson = cur.getInt(index_Person); String strbody = cur.getString(index_Body); long longDate = cur.getLong(index_Date); int intType = cur.getInt(index_Type); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d = new Date(longDate); String strDate = dateFormat.format(d); String strType = ""; if (intType == 1) { strType = "接收"; } else if (intType == 2) { strType = "傳送"; } else { strType = "null"; } smsBuilder.append("[ "); smsBuilder.append(strAddress + ", "); smsBuilder.append(intPerson + ", "); smsBuilder.append(strbody + ", "); smsBuilder.append(strDate + ", "); smsBuilder.append(strType); smsBuilder.append(" ]\n\n"); } while (cur.moveToNext()); if (!cur.isClosed()) { cur.close(); cur = null; } } else { smsBuilder.append("no result!"); } // end if smsBuilder.append("getSmsInPhone has executed!"); } catch (SQLiteException ex) { Log.d("SQLiteException in getSmsInPhone", ex.getMessage()); } return smsBuilder.toString(); } }
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 待發送列表
- _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
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 ,int型別
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_presentTP-Reply-Path
locked
sms資料庫表字段型別的原始碼:
private void createSmsTables(SQLiteDatabase db) {
// N.B.: Whenever the columns here are changed, the columns in
// {@ref MmsSmsProvider} must be changed to match.
db.execSQL("CREATE TABLE sms (" +
"_id INTEGER PRIMARY KEY," +
"thread_id INTEGER," +
"address TEXT," +
"person INTEGER," +
"date INTEGER," +
"date_sent INTEGER DEFAULT 0," +
"protocol INTEGER," +
"read INTEGER DEFAULT 0," +
"status INTEGER DEFAULT -1," + // a TP-Status value
// or -1 if it
// status hasn't
// been received
"type INTEGER," +
"reply_path_present INTEGER," +
"subject TEXT," +
"body TEXT," +
"service_center TEXT," +
"locked INTEGER DEFAULT 0," +
"error_code INTEGER DEFAULT 0," +
"seen INTEGER DEFAULT 0" +
");");
....
}
packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
聯絡人為空
簡訊資料庫裡面如果你是先受到陌生簡訊之後再把陌生人新增到聯絡人列表的話,簡訊資料庫裡面的person欄位就為null,如果你是先新增聯絡人再發簡訊的話,簡訊資料庫裡面的person欄位就不為空了,所以你要是想通過簡訊資料庫裡的欄位取得聯絡人的其他資訊的話,只能通過地址來取。
參考推薦:
(本文轉自http://blog.csdn.net/ithomer/article/details/7328321)