Android SMS(二)—— 讀取簡訊儲存到 SQLite

來源:互聯網
上載者:User

Android 之 SMS 簡訊在Android系統中是儲存在SQLite資料庫中的,但不讓其它程式訪問(Android系統的安全機制)

現在我們在讀取手機內的SMS簡訊,先儲存在我們自己定義的SQLite資料庫中,然後讀取SQLite資料庫提取簡訊,並顯示

SMS簡訊SQLite存取代碼:

package com.homer.sms;</p><p>import java.sql.Date;<br />import java.text.SimpleDateFormat;</p><p>import org.loon.wsi.R;</p><p>import android.app.Activity;<br />import android.content.Context;<br />import android.database.Cursor;<br />import android.database.sqlite.SQLiteDatabase;<br />import android.graphics.Color;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.util.Log;<br />import android.widget.TableLayout;<br />import android.widget.TableRow;<br />import android.widget.TableRow.LayoutParams;<br />import android.widget.TextView;</p><p>/**<br /> * 讀取手機簡訊, 先儲存到SQLite資料,然後再讀取資料庫顯示<br /> *<br /> * @author sunboy_2050<br /> * @since http://blog.csdn.net/sunboy_2050<br /> * @date 2012.03.06<br /> */<br />public class smsRead4 extends Activity {</p><p>TableLayout tableLayout;<br />int index = 0;</p><p>@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);</p><p>setContentView(R.layout.main);</p><p>tableLayout = (TableLayout) findViewById(R.id.tableLayout);<br />showSMS();<br />}</p><p>private void showSMS() {<br />SmsHander smsHander = new SmsHander(this);</p><p>smsHander.createSMSDatabase();// 建立SQLite資料庫<br />smsHander.insertSMSToDatabase();// 讀取手機簡訊,插入SQLite資料庫<br />Cursor cursor = smsHander.querySMSInDatabase(100);// 擷取前100條簡訊(日期排序)</p><p>cursor.moveToPosition(-1);<br />while (cursor.moveToNext()) {<br />String strAddress = cursor.getString(cursor.getColumnIndex("address"));<br />String strDate = cursor.getString(cursor.getColumnIndex("date"));<br />String strBody = cursor.getString(cursor.getColumnIndex("body"));</p><p>SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />Date date = new Date(Long.parseLong(strDate));<br />strDate = dateFormat.format(date);</p><p>String smsTitle = strAddress + "\t\t" + strDate;<br />String smsBody = strBody + "\n";<br />Log.i("tableRow", smsTitle + smsBody);</p><p>// title Row<br />TableRow trTitle = new TableRow(this);<br />trTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));</p><p>TextView tvTitle = new TextView(this);<br />tvTitle.setText(smsTitle);<br />tvTitle.getPaint().setFakeBoldText(true); // 加粗字型<br />tvTitle.setTextColor(Color.RED);<br />tvTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));<br />trTitle.addView(tvTitle);<br />tableLayout.addView(trTitle, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));</p><p>// body Row<br />TableRow trBody = new TableRow(this);<br />trBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));</p><p>TextView tvBody = new TextView(this);<br />tvBody.setText(smsBody);<br />tvBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));<br />trBody.addView(tvBody);<br />tableLayout.addView(trBody, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));<br />}</p><p>if (!cursor.isClosed()) {<br />cursor.close();<br />cursor = null;<br />}</p><p>smsHander.closeSMSDatabase();<br />index = 0;<br />}</p><p>public class SmsHander {</p><p>SQLiteDatabase db;<br />Context context;</p><p>public SmsHander(Context context) {<br />this.context = context;<br />}</p><p>public void createSMSDatabase() {<br />String sql = "create table if not exists sms("<br />+ "_id integer primary key autoincrement,"<br />+ "address varchar(255)," + "person varchar(255),"<br />+ "body varchar(1024)," + "date varchar(255),"<br />+ "type integer)";<br />db = SQLiteDatabase.openOrCreateDatabase(context.getFilesDir().toString() + "/data.db3", null);// 建立資料庫<br />db.execSQL(sql);<br />}</p><p>// 擷取手機簡訊<br />private Cursor getSMSInPhone() {<br />Uri SMS_CONTENT = Uri.parse("content://sms/");<br />String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };<br />Cursor cursor = context.getContentResolver().query(SMS_CONTENT, projection, null, null, "date desc");// 擷取手機簡訊</p><p>while (cursor.moveToNext()) {<br />System.out.println("--sms-- : " + cursor.getString(cursor.getColumnIndex("body")));<br />}</p><p>return cursor;<br />}</p><p>// 儲存手機簡訊到 SQLite 資料庫<br />public void insertSMSToDatabase() {<br />Long lastTime;<br />Cursor dbCount = db.rawQuery("select count(*) from sms", null);<br />dbCount.moveToFirst();<br />if (dbCount.getInt(0) > 0) {<br />Cursor dbcur = db.rawQuery("select * from sms order by date desc limit 1", null);<br />dbcur.moveToFirst();<br />lastTime = Long.parseLong(dbcur.getString(dbcur.getColumnIndex("date")));<br />} else {<br />lastTime = new Long(0);<br />}<br />dbCount.close();<br />dbCount = null;</p><p>Cursor cur = getSMSInPhone(); // 擷取簡訊(遊標)<br />db.beginTransaction(); // 開始交易處理<br />if (cur.moveToFirst()) {<br />String address;<br />String person;<br />String body;<br />String date;<br />int type;</p><p>int iAddress = cur.getColumnIndex("address");<br />int iPerson = cur.getColumnIndex("person");<br />int iBody = cur.getColumnIndex("body");<br />int iDate = cur.getColumnIndex("date");<br />int iType = cur.getColumnIndex("type");</p><p>do {<br />address = cur.getString(iAddress);<br />person = cur.getString(iPerson);<br />body = cur.getString(iBody);<br />date = cur.getString(iDate);<br />type = cur.getInt(iType);</p><p>if (Long.parseLong(date) > lastTime) {<br />String sql = "insert into sms values(null, ?, ?, ?, ?, ?)";<br />Object[] bindArgs = new Object[] { address, person, body, date, type };<br />db.execSQL(sql, bindArgs);<br />} else {<br />break;<br />}<br />} while (cur.moveToNext());</p><p>cur.close();<br />cur = null;<br />db.setTransactionSuccessful(); // 設定交易處理成功,不設定會自動復原不提交<br />db.endTransaction(); // 結束交易處理<br />}</p><p>}</p><p>// 擷取 SQLite 資料庫中的全部簡訊<br />public Cursor querySMSFromDatabase() {<br />String sql = "select * from sms order by date desc";<br />return db.rawQuery(sql, null);<br />}</p><p>// 擷取 SQLite 資料庫中的最新 size 條簡訊<br />public Cursor querySMSInDatabase(int size) {<br />String sql;</p><p>Cursor dbCount = db.rawQuery("select count(*) from sms", null);<br />dbCount.moveToFirst();<br />if (size < dbCount.getInt(0)) { // 不足 size 條簡訊,則取前 size 條<br />sql = "select * from sms order by date desc limit " + size;<br />} else {<br />sql = "select * from sms order by date desc";<br />}<br />dbCount.close();<br />dbCount = null;</p><p>return db.rawQuery(sql, null);<br />}</p><p>// 擷取 SQLite資料庫的前 second秒簡訊<br />public Cursor getSMSInDatabaseFrom(long second) {<br />long time = System.currentTimeMillis() / 1000 - second;<br />String sql = "select * from sms order by date desc where date > " + time;<br />return db.rawQuery(sql, null);<br />}</p><p>// 關閉資料庫<br />public void closeSMSDatabase() {<br />if (db != null && db.isOpen()) {<br />db.close();<br />db = null;<br />}<br />}</p><p>}<br />}<br />運行結果:

程式碼範例

推薦參考:

Android 之 SMS 簡訊讀取

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.