系统短彩信以及会话读取
2014-06-25 22:53
183 查看
转载地址:http://blog.csdn.net/actual_/article/details/8060143
一、需要实现一个同系统信息一样的功能
1)会话列表;
2)会话对话详情;
3)系统会话增加新信息或者删除信息等变化时做到同步;
二、实现思路
通过查询会话表显示会话界面,监听会话数据库实现与系统信息同步。通过会话id查询会话对应的具体聊天内容,比如短信或者彩信;
看下mmssms.db中的uri有好多个,可以根据自己的需求选择合适的uri,可以避免不必要的操作,提高效率。经过多次测试,实现以上功能需要用的信息表有:
1)会话表:content://mms-sms/conversations/
2)短信表:content://sms
3)彩信表:content://mms、content://mms/part
4)信息对应手机号码表:content://mms-sms/canonical-addresses
需要权限:<uses-permission android:name="android.permission.READ_SMS" />
首先:显示会话列表
public static final Uri THREADS_URI = Uri.parse("content://mms-sms/conversations?simple=true");
threads字段:
_id
date
ct_t:区分彩信还是短信:application/vnd.wap.multipart.related是彩信,否则是短信
snippet:最新的一条会话信息。彩信为彩信的主题,短信时短信的body
recipient_ids:信息对应手机号码表(content://mms-sms/canonical-addresses)中的id,查询对应的手机号码
message_count:详细对话条数
read
snippet_cs:snippet的编码方式,彩信:106(utf-8),短信:0
……
根据threads表中的date,snippet,messagecount基本可以显示会话列表了。
另外:
1:通过4)信息对应手机号码表:content://mms-sms/canonical-addresses获取了手机号之后获取联系人姓名以及头像使用:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
2:当会话为彩信时,snippet显示的是彩信的主题,编码方式为utf-8。显示为乱码。
解决:snippet_mms = new String(snippet.getBytes("ISO8859_1"), "utf-8");
其次,获取会话具体对话内容
具体对话的格式有ssm或者mms,通过content://mms-sms/conversations/threadid,可以查询与某联系人的所有聊天记录的id,这个id对应短信或者彩信表中的id。如果具体会话是短信,则去sms表中查询具体内容,如果是彩信则去mms表中查询。通过字段ct_t 区分信息类别。
具体实现:
1)获取sms
[java]
view plaincopy
String selection = "_id = "+id;
Uri uri = Uri.parse("content://sms");
Cursor cursor = contentResolver.query(uri, null, selection, null, null);
String phone = cursor.getString(cursor.getColumnIndex("address"));
int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.
String date = cursor.getString(cursor.getColumnIndex("date"));
String body = cursor.getString(cursor.getColumnIndex("body"));
2)获取mms
彩信表:content://mms
彩信附件:content://mms/part/
[java]
view plaincopy
String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cursor = getContentResolver().query(uri, null,
selectionPart, null, null);
if (cursor.moveToFirst()) {
do {
String partId = cursor.getString(cursor.getColumnIndex("_id"));
String type = cursor.getString(cursor.getColumnIndex("ct"));
if ("text/plain".equals(type)) {
String data = cursor.getString(cPart.getColumnIndex("_data"));
String body;
if (data != null) {
// implementation of this method below
body = getMmsText(partId);
} else {
body = cursor.getString(cursor.getColumnIndex("text"));
}
}
} while (cursor.moveToNext());
}
[java]
view plaincopy
private String getMmsText(String id) {
Uri partURI = Uri.parse("content://mms/part/" + id);
InputStream is = null;
StringBuilder sb = new StringBuilder();
try {
is = getContentResolver().openInputStream(partURI);
if (is != null) {
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader reader = new BufferedReader(isr);
String temp = reader.readLine();
while (temp != null) {
sb.append(temp);
temp = reader.readLine();
}
}
} catch (IOException e) {}
finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {}
}
}
return sb.toString();
}
获取彩信中图片:
[java]
view plaincopy
String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cPart = getContentResolver().query(uri, null,
selectionPart, null, null);
if (cPart.moveToFirst()) {
do {
String partId = cPart.getString(cPart.getColumnIndex("_id"));
String type = cPart.getString(cPart.getColumnIndex("ct"));
if ("image/jpeg".equals(type) || "image/bmp".equals(type) ||
"image/gif".equals(type) || "image/jpg".equals(type) ||
"image/png".equals(type)) {
Bitmap bitmap = getMmsImage(partId);
}
} while (cPart.moveToNext());
}
[java]
view plaincopy
private Bitmap getMmsImage(String _id) {
Uri partURI = Uri.parse("content://mms/part/" + _id);
InputStream is = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(partURI);
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {}
finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {}
}
}
return bitmap;
}
测试结果:
最后,我的会话界面与系统的实时同步
监听系统的会话数据表即可:
public static final Uri THREADS_URI = Uri
.parse("content://mms-sms/conversations?simple=true");
getContentResolver().registerContentObserver(THREADS_URI ,true, sysCallLogObserver);
一、需要实现一个同系统信息一样的功能
1)会话列表;
2)会话对话详情;
3)系统会话增加新信息或者删除信息等变化时做到同步;
二、实现思路
通过查询会话表显示会话界面,监听会话数据库实现与系统信息同步。通过会话id查询会话对应的具体聊天内容,比如短信或者彩信;
看下mmssms.db中的uri有好多个,可以根据自己的需求选择合适的uri,可以避免不必要的操作,提高效率。经过多次测试,实现以上功能需要用的信息表有:
1)会话表:content://mms-sms/conversations/
2)短信表:content://sms
3)彩信表:content://mms、content://mms/part
4)信息对应手机号码表:content://mms-sms/canonical-addresses
需要权限:<uses-permission android:name="android.permission.READ_SMS" />
首先:显示会话列表
public static final Uri THREADS_URI = Uri.parse("content://mms-sms/conversations?simple=true");
threads字段:
_id
date
ct_t:区分彩信还是短信:application/vnd.wap.multipart.related是彩信,否则是短信
snippet:最新的一条会话信息。彩信为彩信的主题,短信时短信的body
recipient_ids:信息对应手机号码表(content://mms-sms/canonical-addresses)中的id,查询对应的手机号码
message_count:详细对话条数
read
snippet_cs:snippet的编码方式,彩信:106(utf-8),短信:0
……
根据threads表中的date,snippet,messagecount基本可以显示会话列表了。
另外:
1:通过4)信息对应手机号码表:content://mms-sms/canonical-addresses获取了手机号之后获取联系人姓名以及头像使用:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
2:当会话为彩信时,snippet显示的是彩信的主题,编码方式为utf-8。显示为乱码。
解决:snippet_mms = new String(snippet.getBytes("ISO8859_1"), "utf-8");
其次,获取会话具体对话内容
具体对话的格式有ssm或者mms,通过content://mms-sms/conversations/threadid,可以查询与某联系人的所有聊天记录的id,这个id对应短信或者彩信表中的id。如果具体会话是短信,则去sms表中查询具体内容,如果是彩信则去mms表中查询。通过字段ct_t 区分信息类别。
具体实现:
1)获取sms
[java]
view plaincopy
String selection = "_id = "+id;
Uri uri = Uri.parse("content://sms");
Cursor cursor = contentResolver.query(uri, null, selection, null, null);
String phone = cursor.getString(cursor.getColumnIndex("address"));
int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.
String date = cursor.getString(cursor.getColumnIndex("date"));
String body = cursor.getString(cursor.getColumnIndex("body"));
2)获取mms
彩信表:content://mms
彩信附件:content://mms/part/
get text content from MMS:
[java] view plaincopy
String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cursor = getContentResolver().query(uri, null,
selectionPart, null, null);
if (cursor.moveToFirst()) {
do {
String partId = cursor.getString(cursor.getColumnIndex("_id"));
String type = cursor.getString(cursor.getColumnIndex("ct"));
if ("text/plain".equals(type)) {
String data = cursor.getString(cPart.getColumnIndex("_data"));
String body;
if (data != null) {
// implementation of this method below
body = getMmsText(partId);
} else {
body = cursor.getString(cursor.getColumnIndex("text"));
}
}
} while (cursor.moveToNext());
}
[java]
view plaincopy
private String getMmsText(String id) {
Uri partURI = Uri.parse("content://mms/part/" + id);
InputStream is = null;
StringBuilder sb = new StringBuilder();
try {
is = getContentResolver().openInputStream(partURI);
if (is != null) {
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader reader = new BufferedReader(isr);
String temp = reader.readLine();
while (temp != null) {
sb.append(temp);
temp = reader.readLine();
}
}
} catch (IOException e) {}
finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {}
}
}
return sb.toString();
}
获取彩信中图片:
[java]
view plaincopy
String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cPart = getContentResolver().query(uri, null,
selectionPart, null, null);
if (cPart.moveToFirst()) {
do {
String partId = cPart.getString(cPart.getColumnIndex("_id"));
String type = cPart.getString(cPart.getColumnIndex("ct"));
if ("image/jpeg".equals(type) || "image/bmp".equals(type) ||
"image/gif".equals(type) || "image/jpg".equals(type) ||
"image/png".equals(type)) {
Bitmap bitmap = getMmsImage(partId);
}
} while (cPart.moveToNext());
}
[java]
view plaincopy
private Bitmap getMmsImage(String _id) {
Uri partURI = Uri.parse("content://mms/part/" + _id);
InputStream is = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(partURI);
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {}
finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {}
}
}
return bitmap;
}
测试结果:
最后,我的会话界面与系统的实时同步
监听系统的会话数据表即可:
public static final Uri THREADS_URI = Uri
.parse("content://mms-sms/conversations?simple=true");
getContentResolver().registerContentObserver(THREADS_URI ,true, sysCallLogObserver);
相关文章推荐
- 【android】 短信彩信以及会话读取
- 【android】 短信彩信以及会话读取
- 关于v$session 和v$process 如何杀掉session(会话),以及如何找到会话对应的系统进程ID
- 从C#中读取硬件以及系统信息
- Oracle查询锁表以及杀会话或系统进程来解除锁表操作
- Oracle查询锁表以及杀会话或系统进程来解除锁表操作
- android学生管理系统,利用linerLayout,实现xml的读取和写入,以及按钮的事件处理
- jar包中读取文件系统绝对路径,相对路径,以及jar包内部路径的写法
- copy文件夹,通过C++读取系统、用户名以及计算机名的方法
- ios读取系统相册以及获取图片的名字
- Python 创建、读取和写入文件以及yield关键字- 千月的python linux 系统管理指南学习笔记(14)
- Swift 读取系统图片以及改变图片 圆形 UIImagePickerController
- 以Android L读取系统所有logcat并写入文件为例分析Android 以添加系统进程的方式申请selinux的权限执行shell脚本,以及avc:dined应该怎么申请权限
- SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存以及将数据放在application
- Linux如何修改环境变量PATH,以及系统登录时读取文件的顺序
- SSH框架---实现系统启动加载类,读取数据库常用数据进入内存以及将数据放在application
- 匆匆的一瞥,错过了一份正确的BIOS……,安装X64系统时错刷BIOS的彻底死机过程以及解决方法
- .net 如何在web.config中读取自定义节点以及注意的问题(转)
- 教学管理系统-会话Bean
- 黄得承的文章__“建设项目的费用控制以及项目费用管理系统的需求”—2005年9月13日