android mms分析,如何在Android中读取MMS数据?

很难找到关于这方面的文档,所以我会在这里收集我找到的所有信息。如果您匆忙或只是不喜欢阅读,跳到如何从SMS中获取数据部分。

内容:/MMS-SMS/会话

这是MMS和SMS提供商..它允许我们同时查询mms和sms数据库,并将它们混合在一个线程中(这些线程称为对话).

为什么URI很重要?这是获取MMS和SMS消息的标准方法;例如,当您收到SMS并单击通知栏时,它将发送如下广播意图:content://mms-sms/conversations/XXX,在哪里XXX是对话的身份。

列出所有谈话的清单

您唯一需要做的就是查询content://mms-sms/conversationsURI:ContentResolver contentResolver = getContentResolver();final String[] projection = new String[]{"*"};

Uri uri = Uri.parse("content://mms-sms/conversations/");Cursor query = contentResolver.query(uri, projection, null, null, null);

注:通常,当你打电话query并希望返回可以传递的所有列。null就像projection参数。但是,您不能在这个提供程序中做到这一点,这就是为什么我要使用*.

现在您可以循环遍历Cursor像往常一样。以下是您想要使用的更重要的列:_id是消息的ID。

显然是营救队长?不怎么有意思。此ID可用于使用以下两种方法检索详细信息

content://sms或

content://mms.

date不需要解释。

thread_id是会话的ID。

body最后一条短信的内容。如果它是一个MMS,即使它有一个文本部分,这将是

null.

注:如果你问content://mms-sms/conversations它将返回一个不同对话的列表_id是每次谈话中的最后一条短信或彩信。如果你问content://mms-sms/conversations/xxx它将返回ID为xxx.

如何区分短消息和彩信

通常,您会想知道您正在处理的是哪种类型的消息。文件上说:虚拟列,MmsSms.TYPE_DISCRIMINATOR_COLUMN,可以在查询的投影中请求。它的值要么是“MMS”,要么是“SMS”,这取决于该行表示的消息分别是MMS消息还是SMS消息。

我想这是指这个变量..然而,我一直未能使它发挥作用。如果你有,请告诉我如何或编辑这篇文章。

到目前为止,这是我所做的,而且似乎奏效了,但必须有更好的办法:ContentResolver contentResolver = getContentResolver();final String[] projection = new String[]{"_id", "ct_t"};

Uri uri = Uri.parse("content://mms-sms/conversations/");Cursor query = contentResolver.query(uri, projection, null, null, null);

if (query.moveToFirst()) {

do {

String string = query.getString(query.getColumnIndex("ct_t"));

if ("application/vnd.wap.multipart.related".equals(string)) {

// it's MMS

} else {

// it's SMS

}

} while (query.moveToNext());}

如何从SMS中获取数据

所以你有短信的ID,那么你唯一要做的就是: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"));

如何从MMS数据中获取数据?

MMSS有点不同。它们可以由不同的部分(文本、音频、图像等)构建;因此,这里将介绍如何分别检索各种数据。

所以让我们猜猜我们的MMS ID在mmsId变量。我们可以使用content://mms/提供者:Uri uri = Uri.parse("content://mms/");String selection = "_id = " + mmsId;Cursor cursor = getContentResolver().query(uri, null, selection, null, null);

然而,唯一有趣的专栏是read那就是1如果消息已经读过了。

如何从MMS中获取文本内容

在这里我们必须使用content://mms/part..例如: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(cursor.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());}

它可以包含文字的不同部分.。但通常只有一个。因此,如果您想要删除循环,它将在大多数情况下工作。这就是为什么getMmsText方法看起来如下: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();}

如何从彩信中获取图像

就像拿到课文一样.。唯一的区别是,您将寻找一种不同的MIME类型: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());}

这就是为什么getMmsImage方法看起来如下: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;}

如何获取发件人地址

您需要使用content://mms/xxx/addr提供者,其中xxx是MMS的id:private String getAddressNumber(int id) {

String selectionAdd = new String("msg_id=" + id);

String uriStr = MessageFormat.format("content://mms/{0}/addr", id);

Uri uriAddress = Uri.parse(uriStr);

Cursor cAdd = getContentResolver().query(uriAddress, null,

selectionAdd, null, null);

String name = null;

if (cAdd.moveToFirst()) {

do {

String number = cAdd.getString(cAdd.getColumnIndex("address"));

if (number != null) {

try {

Long.parseLong(number.replace("-", ""));

name = number;

} catch (NumberFormatException nfe) {

if (name == null) {

name = number;

}

}

}

} while (cAdd.moveToNext());

}

if (cAdd != null) {

cAdd.close();

}

return name;}

最后思想我不明白为什么谷歌拥有成千上万的美元,却不付钱给学生或其他人来记录这个API。您必须检查源代码以了解它是如何工作的,更糟糕的是,它们不公开数据库列中使用的常量,所以我们必须手动编写它们。

对于MMS中的其他类型的数据,您可以应用上面学到的相同的想法.这只是一个了解哑剧类型的问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值