(转)Android SMS(一) —— 读取短信

Android SMS Read

[java]  view plain copy print ?
  1. package com.homer.sms;  
  2.   
  3. import java.sql.Date;  
  4. import java.text.SimpleDateFormat;  
  5.   
  6.   
  7. import android.app.Activity;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteException;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.widget.ScrollView;  
  14. import android.widget.TableLayout;  
  15. import android.widget.TextView;  
  16.   
  17. /** 
  18.  * 读取手机短信 
  19.  *  
  20.  * @author sunboy_2050 
  21.  * @since  http://blog.csdn.net/sunboy_2050 
  22.  * @date   2012.03.06 
  23.  */  
  24. public class smsRead extends Activity {  
  25.   
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.   
  30.         TextView tv = new TextView(this);  
  31.         tv.setText(getSmsInPhone());  
  32.   
  33.         ScrollView sv = new ScrollView(this);  
  34.         sv.addView(tv);  
  35.           
  36.         setContentView(sv);  
  37.     }  
  38.   
  39.     public String getSmsInPhone() {  
  40.         final String SMS_URI_ALL = "content://sms/";  
  41.         final String SMS_URI_INBOX = "content://sms/inbox";  
  42.         final String SMS_URI_SEND = "content://sms/sent";  
  43.         final String SMS_URI_DRAFT = "content://sms/draft";  
  44.         final String SMS_URI_OUTBOX = "content://sms/outbox";  
  45.         final String SMS_URI_FAILED = "content://sms/failed";  
  46.         final String SMS_URI_QUEUED = "content://sms/queued";  
  47.   
  48.         StringBuilder smsBuilder = new StringBuilder();  
  49.   
  50.         try {  
  51.             Uri uri = Uri.parse(SMS_URI_ALL);  
  52.             String[] projection = new String[] { "_id""address""person""body""date""type" };  
  53.             Cursor cur = getContentResolver().query(uri, projection, nullnull"date desc");      // 获取手机内部短信  
  54.   
  55.             if (cur.moveToFirst()) {  
  56.                 int index_Address = cur.getColumnIndex("address");  
  57.                 int index_Person = cur.getColumnIndex("person");  
  58.                 int index_Body = cur.getColumnIndex("body");  
  59.                 int index_Date = cur.getColumnIndex("date");  
  60.                 int index_Type = cur.getColumnIndex("type");  
  61.   
  62.                 do {  
  63.                     String strAddress = cur.getString(index_Address);  
  64.                     int intPerson = cur.getInt(index_Person);  
  65.                     String strbody = cur.getString(index_Body);  
  66.                     long longDate = cur.getLong(index_Date);  
  67.                     int intType = cur.getInt(index_Type);  
  68.   
  69.                     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  70.                     Date d = new Date(longDate);  
  71.                     String strDate = dateFormat.format(d);  
  72.   
  73.                     String strType = "";  
  74.                     if (intType == 1) {  
  75.                         strType = "接收";  
  76.                     } else if (intType == 2) {  
  77.                         strType = "发送";  
  78.                     } else {  
  79.                         strType = "null";  
  80.                     }  
  81.   
  82.                     smsBuilder.append("[ ");  
  83.                     smsBuilder.append(strAddress + ", ");  
  84.                     smsBuilder.append(intPerson + ", ");  
  85.                     smsBuilder.append(strbody + ", ");  
  86.                     smsBuilder.append(strDate + ", ");  
  87.                     smsBuilder.append(strType);  
  88.                     smsBuilder.append(" ]\n\n");  
  89.                 } while (cur.moveToNext());  
  90.   
  91.                 if (!cur.isClosed()) {  
  92.                     cur.close();  
  93.                     cur = null;  
  94.                 }  
  95.             } else {  
  96.                 smsBuilder.append("no result!");  
  97.             } // end if  
  98.   
  99.             smsBuilder.append("getSmsInPhone has executed!");  
  100.   
  101.         } catch (SQLiteException ex) {  
  102.             Log.d("SQLiteException in getSmsInPhone", ex.getMessage());  
  103.         }  
  104.   
  105.         return smsBuilder.toString();  
  106.     }  
  107. }  

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数据库表字段类型的源码:

[java]  view plain copy print ?
  1. private void createSmsTables(SQLiteDatabase db) {  
  2.         // N.B.: Whenever the columns here are changed, the columns in  
  3.         // {@ref MmsSmsProvider} must be changed to match.  
  4.         db.execSQL("CREATE TABLE sms (" +  
  5.                    "_id INTEGER PRIMARY KEY," +  
  6.                    "thread_id INTEGER," +  
  7.                    "address TEXT," +  
  8.                    "person INTEGER," +  
  9.                    "date INTEGER," +  
  10.                    "date_sent INTEGER DEFAULT 0," +  
  11.                    "protocol INTEGER," +  
  12.                    "read INTEGER DEFAULT 0," +  
  13.                    "status INTEGER DEFAULT -1," + // a TP-Status value  
  14.                                                   // or -1 if it  
  15.                                                   // status hasn't  
  16.                                                   // been received  
  17.                    "type INTEGER," +  
  18.                    "reply_path_present INTEGER," +  
  19.                    "subject TEXT," +  
  20.                    "body TEXT," +  
  21.                    "service_center TEXT," +  
  22.                    "locked INTEGER DEFAULT 0," +  
  23.                    "error_code INTEGER DEFAULT 0," +  
  24.                    "seen INTEGER DEFAULT 0" +  
  25.                    ");");  
  26. ....  
  27. }  

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


联系人为空

短信数据库里面如果你是先受到陌生短信之后再把陌生人添加到联系人列表的话,短信数据库里面的person字段就为null,如果你是先添加联系人再发短信的话,短信数据库里面的person字段就不为空了,所以你要是想通过短信数据库里的字段取得联系人的其他信息的话,只能通过地址来取。



参考推荐:

Android SMS Messaging 


转自:http://blog.csdn.net/sunboy_2050/article/details/7328321

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值