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;
-
-
-
-
-
-
-
-
- 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!");
- }
-
- 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 待发送列表
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) {
-
-
- 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," +
-
-
-
- "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(二)—— 读取短信保存到 SQLite
分类: Android
2012-03-07 13:52
3895人阅读
收藏
举报
sms sqlite android date 数据库 string
Android 之 SMS 短信在Android系统中是保存在SQLite数据库中的,但不让其它程序访问(Android系统的安全机制)
现在我们在读取手机内的SMS短信,先保存在我们自己定义的SQLite数据库中,然后读取SQLite数据库提取短信,并显示
SMS短信SQLite存取代码:
- package com.homer.sms;
-
- import java.sql.Date;
- import java.text.SimpleDateFormat;
-
- import org.loon.wsi.R;
-
- import android.app.Activity;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Color;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TableRow.LayoutParams;
- import android.widget.TextView;
-
-
-
-
-
-
-
-
- public class smsRead4 extends Activity {
-
- TableLayout tableLayout;
- int index = 0;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.main);
-
- tableLayout = (TableLayout) findViewById(R.id.tableLayout);
- showSMS();
- }
-
- private void showSMS() {
- SmsHander smsHander = new SmsHander(this);
-
- smsHander.createSMSDatabase();
- smsHander.insertSMSToDatabase();
- Cursor cursor = smsHander.querySMSInDatabase(100);
-
- cursor.moveToPosition(-1);
- while (cursor.moveToNext()) {
- String strAddress = cursor.getString(cursor.getColumnIndex("address"));
- String strDate = cursor.getString(cursor.getColumnIndex("date"));
- String strBody = cursor.getString(cursor.getColumnIndex("body"));
-
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = new Date(Long.parseLong(strDate));
- strDate = dateFormat.format(date);
-
- String smsTitle = strAddress + "\t\t" + strDate;
- String smsBody = strBody + "\n";
- Log.i("tableRow", smsTitle + smsBody);
-
-
- TableRow trTitle = new TableRow(this);
- trTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
-
- TextView tvTitle = new TextView(this);
- tvTitle.setText(smsTitle);
- tvTitle.getPaint().setFakeBoldText(true);
- tvTitle.setTextColor(Color.RED);
- tvTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- trTitle.addView(tvTitle);
- tableLayout.addView(trTitle, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
-
-
- TableRow trBody = new TableRow(this);
- trBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
-
- TextView tvBody = new TextView(this);
- tvBody.setText(smsBody);
- tvBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- trBody.addView(tvBody);
- tableLayout.addView(trBody, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- }
-
- if (!cursor.isClosed()) {
- cursor.close();
- cursor = null;
- }
-
- smsHander.closeSMSDatabase();
- index = 0;
- }
-
- public class SmsHander {
-
- SQLiteDatabase db;
- Context context;
-
- public SmsHander(Context context) {
- this.context = context;
- }
-
- public void createSMSDatabase() {
- String sql = "create table if not exists sms("
- + "_id integer primary key autoincrement,"
- + "address varchar(255)," + "person varchar(255),"
- + "body varchar(1024)," + "date varchar(255),"
- + "type integer)";
- db = SQLiteDatabase.openOrCreateDatabase(context.getFilesDir().toString() + "/data.db3", null);
- db.execSQL(sql);
- }
-
-
- private Cursor getSMSInPhone() {
- Uri SMS_CONTENT = Uri.parse("content://sms/");
- String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
- Cursor cursor = context.getContentResolver().query(SMS_CONTENT, projection, null, null, "date desc");
-
- while (cursor.moveToNext()) {
- System.out.println("--sms-- : " + cursor.getString(cursor.getColumnIndex("body")));
- }
-
- return cursor;
- }
-
-
- public void insertSMSToDatabase() {
- Long lastTime;
- Cursor dbCount = db.rawQuery("select count(*) from sms", null);
- dbCount.moveToFirst();
- if (dbCount.getInt(0) > 0) {
- Cursor dbcur = db.rawQuery("select * from sms order by date desc limit 1", null);
- dbcur.moveToFirst();
- lastTime = Long.parseLong(dbcur.getString(dbcur.getColumnIndex("date")));
- } else {
- lastTime = new Long(0);
- }
- dbCount.close();
- dbCount = null;
-
- Cursor cur = getSMSInPhone();
- db.beginTransaction();
- if (cur.moveToFirst()) {
- String address;
- String person;
- String body;
- String date;
- int type;
-
- int iAddress = cur.getColumnIndex("address");
- int iPerson = cur.getColumnIndex("person");
- int iBody = cur.getColumnIndex("body");
- int iDate = cur.getColumnIndex("date");
- int iType = cur.getColumnIndex("type");
-
- do {
- address = cur.getString(iAddress);
- person = cur.getString(iPerson);
- body = cur.getString(iBody);
- date = cur.getString(iDate);
- type = cur.getInt(iType);
-
- if (Long.parseLong(date) > lastTime) {
- String sql = "insert into sms values(null, ?, ?, ?, ?, ?)";
- Object[] bindArgs = new Object[] { address, person, body, date, type };
- db.execSQL(sql, bindArgs);
- } else {
- break;
- }
- } while (cur.moveToNext());
-
- cur.close();
- cur = null;
- db.setTransactionSuccessful();
- db.endTransaction();
- }
-
- }
-
-
- public Cursor querySMSFromDatabase() {
- String sql = "select * from sms order by date desc";
- return db.rawQuery(sql, null);
- }
-
-
- public Cursor querySMSInDatabase(int size) {
- String sql;
-
- Cursor dbCount = db.rawQuery("select count(*) from sms", null);
- dbCount.moveToFirst();
- if (size < dbCount.getInt(0)) {
- sql = "select * from sms order by date desc limit " + size;
- } else {
- sql = "select * from sms order by date desc";
- }
- dbCount.close();
- dbCount = null;
-
- return db.rawQuery(sql, null);
- }
-
-
- public Cursor getSMSInDatabaseFrom(long second) {
- long time = System.currentTimeMillis() / 1000 - second;
- String sql = "select * from sms order by date desc where date > " + time;
- return db.rawQuery(sql, null);
- }
-
-
- public void closeSMSDatabase() {
- if (db != null && db.isOpen()) {
- db.close();
- db = null;
- }
- }
-
- }
- }
运行结果:
代码示例
推荐参考:
Android 之 SMS 短信读取