Android 手機信息存放在mmssms.db數據庫,位於data/data/com.android.providers.telephony/databases下。java
短訊息主要用到sms表和threads表。android
sms表
sql
threads表數據庫
sms表和threads表用thread_id互連;異步
sms表中重要的列位:ide
_id sms表自增列,查詢時必須加上函數
thread_id 列 對應着threads表url
date 爲時間spa
body 爲短信內容code
address 爲電話號碼列
type 短信類型;接收、發送、草稿箱等 1接收 2發送
threads表中重要的列
_id對應sms表中thread_id
data 時間
message_count 短信的數量
snippet 短信內容
短信異步查詢簡介
//要查詢的列 private static final String[] CONVERSATION_PROJECTION = new String[]{ "sms.thread_id AS _id", "groups.msg_count AS msg_count", "sms.body AS snippet", "sms.address AS address", "sms.date AS date" }; //對應查詢列的位置 private static final int ID_COLUMN_INDEX = 0; private static final int MSG_COUNT_COLUMN_INDEX = 1; private static final int SNIPPET_COLUMN_INDEX = 2; private static final int ADDRESS_COLUMN_INDEX = 3; private static final int DATE_COLUMN_INDEX = 4; //查詢 Uri uri = Uri.parse("content://sms/conversations"); queryHandler.startQuery(0, null, uri, CONVERSATION_PROJECTION, null, null, " date DESC"); //異步查詢 等同於 Cursor cursor = cr.query(uri,CONVERSATION_PROJECTION, null, null, null, null, " date DESC", null); 從uri表中查找出CONVERSATION_PROJECTION列,而後按照數據遞減方式排序 //得到查詢的數據 String idStr = cursor.getString(ID_COLUMN_INDEX); //得到ID_COLUMN_INDEX列的值 即thread_id int msg_count = cursor.getInt(MSG_COUNT_COLUMN_INDEX); //得到 msg_count String body = cursor.getString(SNIPPET_COLUMN_INDEX); //得到 snippet long date = cursor.getLong(DATE_COLUMN_INDEX); //得到 date String address = cursor.getString(ADDRESS_COLUMN_INDEX); //得到 address 短信刪除 Uri url = Uri.withAppendedPath(Sms.CONVERSATION_URI, thread_id); withAppendedPath()返回指定行號 即_id 的uri getContentResolver().delete(url, null, null);
新建一個uri 在通信錄中找到 電話號碼對應的行 並返回uri String contactName = null; Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)); Cursor contactCursor = getContentResolver().query(uri, CONTACT_PROJECTION, null, null, null); if(contactCursor.moveToFirst()){ contactName = contactCursor.getString(DISPLAY_NAME_COLUMN_INDEX); } contactCursor.close(); private static final String[] CONTACT_PROJECTION = new String[] { ContactsContract.Contacts._ID, //這個表 不加_id也能夠,加了也不錯,爲了統一加了 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME //電話中 姓名 }; private static final int DISPLAY_NAME_COLUMN_INDEX = 1; //姓名
同步查詢 //這種方式容易讓程序等待時間過長,因此 不採用
Cursor cursor = db.rawQuery(「select * from person」, null); while (cursor.moveToNext()) { int personid = cursor.getInt(0); //獲取第一列的值,第一列的索引從0開始 String name = cursor.getString(1);//獲取第二列的值 int age = cursor.getInt(2);//獲取第三列的值 } cursor.close(); db.close();
private static final String[] SMS_PROJECTION = new String[]{ "_id", //表的 id "address", //電話號碼 "date", //時間 "type", //短信類型 "body" //內容 }; String selection = " thread_id = ?"; String[] selectionArgs = new String[]{thread_id}; //上面的id queryHandler.startQuery(0, null, Uri.parse("content://sms"), SMS_PROJECTION, selection, selectionArgs , " date DESC");
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) String table = "Orders" ; //表 String[] columns = new String[] { "CustomerName" , "OrderPrice" }; //查詢列 String selection = "Orderdate>?" ; //選擇條件 String[] selectionArgs = new String[]{ "2012" }; //選擇條件 String groupBy = "CustomerName" ; //組名 String having = "SUM(OrderPrice)>500" ; String orderBy = "CustomerName" ; Cursor c = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, " OrderPrice DESC"); 等同於下面sql語句 SELECT CustomerName, OrderPrice FROM Orders WHERE Orderdate>2012 GROUP BY CustomerName HAVING OrderPrice>500 ORDER BY OrderPrice DESC 從Orders表中查詢 CustomerName, OrderPrice 條件 Orderdate>2012 以 CustomerName 而且 OrderPrice>500 進行分組 最後 OrderPrice 遞減方式進行排序