Android編程之SMS讀取短信并保存到SQLite的方法
本文實(shí)例講述了Android編程之SMS讀取短信并保存到SQLite的方法。分享給大家供大家參考,具體如下:
Android 之 SMS 短信在Android系統(tǒng)中是保存在SQLite數(shù)據(jù)庫中的,但不讓其它程序訪問(Android系統(tǒng)的安全機(jī)制)
現(xiàn)在我們在讀取手機(jī)內(nèi)的SMS短信,先保存在我們自己定義的SQLite數(shù)據(jù)庫中,然后讀取SQLite數(shù)據(jù)庫提取短信,并顯示
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; /** * 讀取手機(jī)短信, 先保存到SQLite數(shù)據(jù),然后再讀取數(shù)據(jù)庫顯示 * * @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(); // 創(chuàng)建SQLite數(shù)據(jù)庫 smsHander.insertSMSToDatabase(); // 讀取手機(jī)短信,插入SQLite數(shù)據(jù)庫 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); // 創(chuàng)建數(shù)據(jù)庫 db.execSQL(sql); } // 獲取手機(jī)短信 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"); // 獲取手機(jī)短信 while (cursor.moveToNext()) { System.out.println("--sms-- : " + cursor.getString(cursor.getColumnIndex("body"))); } return cursor; } // 保存手機(jī)短信到 SQLite 數(shù)據(jù)庫 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(); // 獲取短信(游標(biāo)) db.beginTransaction(); // 開始事務(wù)處理 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(); // 設(shè)置事務(wù)處理成功,不設(shè)置會自動回滾不提交 db.endTransaction(); // 結(jié)束事務(wù)處理 } } // 獲取 SQLite 數(shù)據(jù)庫中的全部短信 public Cursor querySMSFromDatabase() { String sql = "select * from sms order by date desc"; return db.rawQuery(sql, null); } // 獲取 SQLite 數(shù)據(jù)庫中的最新 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數(shù)據(jù)庫的前 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); } // 關(guān)閉數(shù)據(jù)庫 public void closeSMSDatabase() { if (db != null && db.isOpen()) { db.close(); db = null; } } } }
運(yùn)行結(jié)果:
完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android組件Glide實(shí)現(xiàn)圖片平滑滾動效果
這篇文章主要介紹了Android組件Glide實(shí)現(xiàn)圖片平滑滾動效果的相關(guān)資料,具有一定的參考價(jià)值,需要的朋友可以參考下2016-07-07Android補(bǔ)間動畫基本使用(位移、縮放、旋轉(zhuǎn)、透明)
這篇文章主要介紹了Android補(bǔ)間動畫基本使用(位移、縮放、旋轉(zhuǎn)、透明),補(bǔ)間動畫就是原形態(tài)變成新形態(tài)時(shí)為了過渡變形過程,生成的動畫2018-05-05使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂胓enymotion訪問本地上Tomcat上數(shù)據(jù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android實(shí)現(xiàn)帶磁性的懸浮窗體效果
這篇文章主要介紹了Android實(shí)現(xiàn)帶磁性的懸浮窗體效果,涉及Android針對窗體的動態(tài)操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android開發(fā)之InetAddress基礎(chǔ)入門簡介與源碼實(shí)例
這篇文章主要介紹了Android開發(fā)之InetAddress基礎(chǔ)入門簡介,需要的朋友可以參考下2020-03-03Kotlin自定義View系列教程之標(biāo)尺控件(選擇身高、體重等)的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Kotlin自定義View系列教程之標(biāo)尺控件(選擇身高、體重等)實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Android ndk獲取手機(jī)內(nèi)部存儲卡的根目錄方法
今天小編就為大家分享一篇Android ndk獲取手機(jī)內(nèi)部存儲卡的根目錄方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android Studio安裝配置、環(huán)境搭建詳細(xì)步驟及基本使用的詳細(xì)教程
這篇文章主要介紹了Android Studio安裝配置、環(huán)境搭建詳細(xì)步驟及基本使用的詳細(xì)教程,需要的朋友可以參考下2020-03-03Android設(shè)置個性化Dialog小圖標(biāo)的方法
這篇文章主要介紹了Android設(shè)置個性化Dialog小圖標(biāo)的方法,涉及Android針對系統(tǒng)資源的設(shè)置與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2016-08-08