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 {@Overridepublic 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 ifsmsBuilder.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-permissionandroid: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 => 协议0SMS_RPOTO,1MMS_PROTO
  7. read => 是否阅读0未读,1已读
  8. status => 状态 -1接收,0complete,64pending,128failed
  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 协议,分为:0SMS_RPOTO,1MMS_PROTO
read 是否阅读0未读,1已读
status 状态-1接收,0complete,64pending,128failed
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) {        // 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字段就不为空了,所以你要是想通过短信数据库里的字段取得联系人的其他信息的话,只能通过地址来取。



参考推荐:

Android SMS Messaging


更多相关文章

  1. Android短信接收
  2. Android中短信拦截解决方案
  3. android获取短信state
  4. android 发送短信的两种方式
  5. android 发送短信的两种方式
  6. Android下调用收发短信邮件等
  7. 1.5 Android(安卓)入门实例 后台循环发短信
  8. Android发送短信
  9. Android短信发送器实现方法

随机推荐

  1. Android中输出HTML格式下的文字
  2. android之自定义控件一控件的呈现机制
  3. 用百度API高仿微信定位demo
  4. [置顶] Android(安卓)listview checkbox
  5. (android/swig实现)用c/c++混合编程方式
  6. Android 模拟HTTP 协议进行表单提交
  7. Eclipse下Android的代码调试
  8. Android 系统(243)---Android进程系列第一
  9. win10通过网线连接树莓派和PC方法(Android
  10. android支持多种屏幕