Android SMS(二)—— 读取短信保存到 SQLite

来源:互联网 发布:java编写一个学生类 编辑:程序博客网 时间:2024/06/05 18:04

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

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


SMS短信SQLite存取代码:

[java] view plaincopyprint?
  1. package com.homer.sms;  
  2.   
  3. import java.sql.Date;  
  4. import java.text.SimpleDateFormat;  
  5.   
  6. import org.loon.wsi.R;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.database.Cursor;  
  11. import android.database.sqlite.SQLiteDatabase;  
  12. import android.graphics.Color;  
  13. import android.net.Uri;  
  14. import android.os.Bundle;  
  15. import android.util.Log;  
  16. import android.widget.TableLayout;  
  17. import android.widget.TableRow;  
  18. import android.widget.TableRow.LayoutParams;  
  19. import android.widget.TextView;  
  20.   
  21. /** 
  22.  * 读取手机短信, 先保存到SQLite数据,然后再读取数据库显示 
  23.  *  
  24.  * @author sunboy_2050 
  25.  * @since  http://blog.csdn.net/sunboy_2050 
  26.  * @date   2012.03.06 
  27.  */  
  28. public class smsRead4 extends Activity {  
  29.   
  30.     TableLayout tableLayout;  
  31.     int index = 0;  
  32.   
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.   
  37.         setContentView(R.layout.main);  
  38.   
  39.         tableLayout = (TableLayout) findViewById(R.id.tableLayout);  
  40.         showSMS();  
  41.     }  
  42.   
  43.     private void showSMS() {  
  44.         SmsHander smsHander = new SmsHander(this);  
  45.   
  46.         smsHander.createSMSDatabase();                          // 创建SQLite数据库  
  47.         smsHander.insertSMSToDatabase();                        // 读取手机短信,插入SQLite数据库  
  48.         Cursor cursor = smsHander.querySMSInDatabase(100);      // 获取前100条短信(日期排序)  
  49.   
  50.         cursor.moveToPosition(-1);  
  51.         while (cursor.moveToNext()) {  
  52.             String strAddress = cursor.getString(cursor.getColumnIndex("address"));  
  53.             String strDate = cursor.getString(cursor.getColumnIndex("date"));  
  54.             String strBody = cursor.getString(cursor.getColumnIndex("body"));  
  55.   
  56.             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  57.             Date date = new Date(Long.parseLong(strDate));  
  58.             strDate = dateFormat.format(date);  
  59.   
  60.             String smsTitle = strAddress + "\t\t" + strDate;  
  61.             String smsBody = strBody + "\n";  
  62.             Log.i("tableRow", smsTitle + smsBody);  
  63.   
  64.             // title Row   
  65.             TableRow trTitle = new TableRow(this);  
  66.             trTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  67.   
  68.             TextView tvTitle = new TextView(this);  
  69.             tvTitle.setText(smsTitle);  
  70.             tvTitle.getPaint().setFakeBoldText(true); // 加粗字体  
  71.             tvTitle.setTextColor(Color.RED);  
  72.             tvTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  73.             trTitle.addView(tvTitle);  
  74.             tableLayout.addView(trTitle, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  75.   
  76.             // body Row   
  77.             TableRow trBody = new TableRow(this);  
  78.             trBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  79.   
  80.             TextView tvBody = new TextView(this);  
  81.             tvBody.setText(smsBody);  
  82.             tvBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  83.             trBody.addView(tvBody);  
  84.             tableLayout.addView(trBody, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  85.         }  
  86.   
  87.         if (!cursor.isClosed()) {  
  88.             cursor.close();  
  89.             cursor = null;  
  90.         }  
  91.   
  92.         smsHander.closeSMSDatabase();  
  93.         index = 0;  
  94.     }  
  95.   
  96.     public class SmsHander {  
  97.   
  98.         SQLiteDatabase db;  
  99.         Context context;  
  100.   
  101.         public SmsHander(Context context) {  
  102.             this.context = context;  
  103.         }  
  104.   
  105.         public void createSMSDatabase() {  
  106.             String sql = "create table if not exists sms("  
  107.                     + "_id integer primary key autoincrement,"  
  108.                     + "address varchar(255)," + "person varchar(255),"  
  109.                     + "body varchar(1024)," + "date varchar(255),"  
  110.                     + "type integer)";  
  111.             db = SQLiteDatabase.openOrCreateDatabase(context.getFilesDir().toString() + "/data.db3"null);         // 创建数据库  
  112.             db.execSQL(sql);  
  113.         }  
  114.   
  115.         // 获取手机短信   
  116.         private Cursor getSMSInPhone() {  
  117.             Uri SMS_CONTENT = Uri.parse("content://sms/");  
  118.             String[] projection = new String[] { "_id""address""person""body""date""type" };  
  119.             Cursor cursor = context.getContentResolver().query(SMS_CONTENT, projection, nullnull"date desc");   // 获取手机短信  
  120.   
  121.             while (cursor.moveToNext()) {  
  122.                 System.out.println("--sms-- : " + cursor.getString(cursor.getColumnIndex("body")));  
  123.             }  
  124.   
  125.             return cursor;  
  126.         }  
  127.   
  128.         // 保存手机短信到 SQLite 数据库   
  129.         public void insertSMSToDatabase() {  
  130.             Long lastTime;  
  131.             Cursor dbCount = db.rawQuery("select count(*) from sms"null);  
  132.             dbCount.moveToFirst();  
  133.             if (dbCount.getInt(0) > 0) {  
  134.                 Cursor dbcur = db.rawQuery("select * from sms order by date desc limit 1"null);  
  135.                 dbcur.moveToFirst();  
  136.                 lastTime = Long.parseLong(dbcur.getString(dbcur.getColumnIndex("date")));  
  137.             } else {  
  138.                 lastTime = new Long(0);  
  139.             }  
  140.             dbCount.close();  
  141.             dbCount = null;  
  142.   
  143.             Cursor cur = getSMSInPhone(); // 获取短信(游标)  
  144.             db.beginTransaction(); // 开始事务处理  
  145.             if (cur.moveToFirst()) {  
  146.                 String address;  
  147.                 String person;  
  148.                 String body;  
  149.                 String date;  
  150.                 int type;  
  151.   
  152.                 int iAddress = cur.getColumnIndex("address");  
  153.                 int iPerson = cur.getColumnIndex("person");  
  154.                 int iBody = cur.getColumnIndex("body");  
  155.                 int iDate = cur.getColumnIndex("date");  
  156.                 int iType = cur.getColumnIndex("type");  
  157.   
  158.                 do {  
  159.                     address = cur.getString(iAddress);  
  160.                     person = cur.getString(iPerson);  
  161.                     body = cur.getString(iBody);  
  162.                     date = cur.getString(iDate);  
  163.                     type = cur.getInt(iType);  
  164.   
  165.                     if (Long.parseLong(date) > lastTime) {  
  166.                         String sql = "insert into sms values(null, ?, ?, ?, ?, ?)";  
  167.                         Object[] bindArgs = new Object[] { address, person, body, date, type };  
  168.                         db.execSQL(sql, bindArgs);  
  169.                     } else {  
  170.                         break;  
  171.                     }  
  172.                 } while (cur.moveToNext());  
  173.   
  174.                 cur.close();  
  175.                 cur = null;  
  176.                 db.setTransactionSuccessful();  // 设置事务处理成功,不设置会自动回滚不提交  
  177.                 db.endTransaction();            // 结束事务处理  
  178.             }  
  179.   
  180.         }  
  181.   
  182.         // 获取 SQLite 数据库中的全部短信   
  183.         public Cursor querySMSFromDatabase() {  
  184.             String sql = "select * from sms order by date desc";  
  185.             return db.rawQuery(sql, null);  
  186.         }  
  187.   
  188.         // 获取 SQLite 数据库中的最新 size 条短信   
  189.         public Cursor querySMSInDatabase(int size) {  
  190.             String sql;  
  191.   
  192.             Cursor dbCount = db.rawQuery("select count(*) from sms"null);  
  193.             dbCount.moveToFirst();  
  194.             if (size < dbCount.getInt(0)) { // 不足 size 条短信,则取前 size 条  
  195.                 sql = "select * from sms order by date desc limit " + size;  
  196.             } else {  
  197.                 sql = "select * from sms order by date desc";  
  198.             }  
  199.             dbCount.close();  
  200.             dbCount = null;  
  201.   
  202.             return db.rawQuery(sql, null);  
  203.         }  
  204.   
  205.         // 获取 SQLite数据库的前 second秒短信  
  206.         public Cursor getSMSInDatabaseFrom(long second) {  
  207.             long time = System.currentTimeMillis() / 1000 - second;  
  208.             String sql = "select * from sms order by date desc where date > " + time;  
  209.             return db.rawQuery(sql, null);  
  210.         }  
  211.   
  212.         // 关闭数据库   
  213.         public void closeSMSDatabase() {  
  214.             if (db != null && db.isOpen()) {  
  215.                 db.close();  
  216.                 db = null;  
  217.             }  
  218.         }  
  219.   
  220.     }  
  221. }  
运行结果:


代码示例


推荐参考:

Android 之 SMS 短信读取

原创粉丝点击