package com.homer.sms; import java.sql.Date;import java.text.SimpleDateFormat; import android.app.Activity;import android.database.Cursor;importandroid.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 publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextViewtv = new TextView(this); tv.setText(getSmsInPhone()); ScrollViewsv = new ScrollView(this); sv.addView(tv); setContentView(sv); } publicString getSmsInPhone() { finalString SMS_URI_ALL = "content://sms/"; finalString SMS_URI_INBOX = "content://sms/inbox"; finalString SMS_URI_SEND = "content://sms/sent"; finalString SMS_URI_DRAFT = "content://sms/draft"; finalString SMS_URI_OUTBOX = "content://sms/outbox"; finalString SMS_URI_FAILED = "content://sms/failed"; finalString SMS_URI_QUEUED = "content://sms/queued"; StringBuildersmsBuilder = new StringBuilder(); try{ Uriuri = Uri.parse(SMS_URI_ALL); String[]projection = new String[] { "_id", "address","person", "body", "date", "type" }; Cursorcur = getContentResolver().query(uri, projection, null, null, "datedesc"); // 获取手机内部短信 if(cur.moveToFirst()) { intindex_Address = cur.getColumnIndex("address"); // 获取sms数据库字段 intindex_Person = cur.getColumnIndex("person"); intindex_Body = cur.getColumnIndex("body"); intindex_Date = cur.getColumnIndex("date"); intindex_Type = cur.getColumnIndex("type"); do{ StringstrAddress = cur.getString(index_Address); intintPerson = cur.getInt(index_Person); Stringstrbody = cur.getString(index_Body); longlongDate = cur.getLong(index_Date); intintType = cur.getInt(index_Type); SimpleDateFormatdateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Dated = new Date(longDate); StringstrDate = dateFormat.format(d); StringstrType = ""; // 接收/发送(1/2)形式转换 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("noresult!"); }// end if smsBuilder.append("getSmsInPhonehas executed!"); }catch (SQLiteException ex) { Log.d("SQLiteExceptionin getSmsInPhone", ex.getMessage()); } returnsmsBuilder.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 待发送列表
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-Pathlocked
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" +
");");
....}
详见Android源码:packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
短信数据库里面如果你是先受到陌生短信之后再把陌生人添加到联系人列表的话,短信数据库里面的person字段就为null
如果你是先添加联系人再发短信的话,短信数据库里面的person字段就不为空了
所以你要是想通过短信数据库里的字段取得联系人的其他信息的话,只能通过地址来取。
转自: