2019独角兽企业重金招聘Python工程师标准>>>
Android 手机信息存放在mmssms.db数据库,位于data/data/com.android.providers.telephony/databases下。
短讯息主要用到sms表和threads表。
sms表
threads表
sms表和threads表用thread_id互连;
sms表中重要的列位:
_id sms表自增列,查询时必须加上
thread_id 列 对应着threads表
date 为时间
body 为短信内容
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 递减方式进行排序