Android编程之SMS读取短信并保存到SQLite的方法

时间:2022-05-09 07:17:03

本文实例讲述了Android编程之SMS读取短信并保存到SQLite的方法。分享给大家供大家参考,具体如下:

Android 之 SMS 短信在Android系统中是保存在SQLite数据库中的,但不让其它程序访问(Android系统的安全机制)

现在我们在读取手机内的SMS短信,先保存在我们自己定义的SQLite数据库中,然后读取SQLite数据库提取短信,并显示

SMS短信SQLite存取代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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;
/**
 * 读取手机短信, 先保存到SQLite数据,然后再读取数据库显示
 *
 * @author sunboy_2050
 * @since http://blog.csdn.net/sunboy_2050
 * @date 2012.03.06
 */
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(); // 创建SQLite数据库
  smsHander.insertSMSToDatabase(); // 读取手机短信,插入SQLite数据库
  Cursor cursor = smsHander.querySMSInDatabase(100); // 获取前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);
   // title Row
   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));
   // body Row
   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;
  }
  // 保存手机短信到 SQLite 数据库
  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(); // 结束事务处理
   }
  }
  // 获取 SQLite 数据库中的全部短信
  public Cursor querySMSFromDatabase() {
   String sql = "select * from sms order by date desc";
   return db.rawQuery(sql, null);
  }
  // 获取 SQLite 数据库中的最新 size 条短信
  public Cursor querySMSInDatabase(int size) {
   String sql;
   Cursor dbCount = db.rawQuery("select count(*) from sms", null);
   dbCount.moveToFirst();
   if (size < dbCount.getInt(0)) { // 不足 size 条短信,则取前 size 条
    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);
  }
  // 获取 SQLite数据库的前 second秒短信
  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读取短信并保存到SQLite的方法

完整实例代码代码点击此处本站下载

希望本文所述对大家Android程序设计有所帮助。