彩信数据总是错误
MMS Date is alwasy wrong
我正在从我的 Galaxy S6 读取短信和彩信数据。所有 SMS 消息都有一个 date
字段,例如 1456252633000
。该数字是 unix 时间戳 * 1000。我发送(未收到)的 MMS 消息具有如下日期字段: 1440628863
时间戳在 unix 时间格式中是正确的。但是,我收到的 MMS 消息的时间戳完全关闭,如下所示:1454881665
.
上面最后两个时间戳是连续的,但第二个时间戳显示好像是几个月前。为什么会这样?我这里有什么地方做错了吗?
编辑
我读了这篇文章 (How do the retrieve the date of the mms from content://mms.),发现我的问题与此类似。日期停留在 1970 年。但即使将其转换为毫秒 (1440185636 * 1000
),我仍然得到错误的日期...
编辑 2
我正在尝试使用 longs 值进行转换,但是,日期仍然显示为关闭月数。
供参考,时间是 2/9/2016。当我进行转换时,我得到 8/26/2015。参考时间戳为:1440603051L * 1000L
.
编辑 3
这是一些代码:
ContentResolver contentResolver = context.getContentResolver();
Cursor c = contentResolver.query(Telephony.Mms.CONTENT_URI, null, filter, null, null);
JSONArray array = new JSONArray();
try {
if (c.moveToFirst()) {
do {
try {
String[] cols = c.getColumnNames();
String id = c.getString(c.getColumnIndexOrThrow("_id"));
JSONObject msg = populateFromMmsPart(id);
if (msg != null) {
msg.put("id", id);
msg.put("read", c.getString(c.getColumnIndexOrThrow("read")).contains("1"));
msg.put("time", c.getString(c.getColumnIndexOrThrow("date")));
msg.put("m_id", c.getString(c.getColumnIndexOrThrow("m_id")));
msg.put("received", c.getString(c.getColumnIndexOrThrow("msg_box")).contains("1"));
msg.put("thread_id", c.getString(c.getColumnIndexOrThrow("thread_id")));
array.put(msg);
}
} catch (JSONException j) {
j.printStackTrace();
}
} while (c.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
}
private JSONObject populateFromMmsPart(String msgId) {
ContentResolver contentResolver = context.getContentResolver();
Uri messages = Uri.parse("content://mms/part");
Cursor c = contentResolver.query(messages, null, "_id=" + msgId, null, null);
JSONObject msg = null;
if (c.moveToFirst()) {
try {
String mid = c.getString(c.getColumnIndex("mid"));
String type = c.getString(c.getColumnIndex("ct"));
if (!"application/smil".equals(type)) {
// Put all the addresses in one place
msg = new JSONObject();
msg.put("address", getAddressesForMmsMessages(mid));
msg.put("mid", mid);
msg.put("type", type);
if ("text/plain".equals(type)) {
// MMS is just plain text, so get the text
String data = c.getString(c.getColumnIndex("_data"));
String body;
if (data != null) {
body = getMmsText(msgId);
} else {
body = c.getString(c.getColumnIndex("text"));
}
msg.put("msg", body);
}
}
} catch (JSONException j) {
j.printStackTrace();
}
}
c.close();
return msg;
}
之前我在做:
ContentResolver contentResolver = context.getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, "thread_id=109", null, null);
我收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
在 populateFromMmsPart()
方法中,您正在查询 part
table 中的记录,其 _id
与 mms
table。但是,part
table 中的 _id
是部件的 ID,而不是消息。消息 ID 在 mid
列中。当消息 ID 恰好与旧消息的部分 ID 匹配时,这显然会导致混淆。
按如下方式更改您的查询。
Cursor c = contentResolver.query(messages, null, "mid=" + msgId, null, null);
此外,在 populateFromMmsPart()
方法中,您只处理返回的 Cursor
的第一行。 Cursor
将(至少)有三行。一个是 application/smil
,一个是 text/plain
,另一个是附件的 MIME 类型。您需要遍历 Cursor
来获取它们。正如您现在所拥有的那样,您只是调用 c.moveToFirst()
,并处理那一行。
我正在从我的 Galaxy S6 读取短信和彩信数据。所有 SMS 消息都有一个 date
字段,例如 1456252633000
。该数字是 unix 时间戳 * 1000。我发送(未收到)的 MMS 消息具有如下日期字段: 1440628863
时间戳在 unix 时间格式中是正确的。但是,我收到的 MMS 消息的时间戳完全关闭,如下所示:1454881665
.
上面最后两个时间戳是连续的,但第二个时间戳显示好像是几个月前。为什么会这样?我这里有什么地方做错了吗?
编辑
我读了这篇文章 (How do the retrieve the date of the mms from content://mms.),发现我的问题与此类似。日期停留在 1970 年。但即使将其转换为毫秒 (1440185636 * 1000
),我仍然得到错误的日期...
编辑 2
我正在尝试使用 longs 值进行转换,但是,日期仍然显示为关闭月数。
供参考,时间是 2/9/2016。当我进行转换时,我得到 8/26/2015。参考时间戳为:1440603051L * 1000L
.
编辑 3
这是一些代码:
ContentResolver contentResolver = context.getContentResolver();
Cursor c = contentResolver.query(Telephony.Mms.CONTENT_URI, null, filter, null, null);
JSONArray array = new JSONArray();
try {
if (c.moveToFirst()) {
do {
try {
String[] cols = c.getColumnNames();
String id = c.getString(c.getColumnIndexOrThrow("_id"));
JSONObject msg = populateFromMmsPart(id);
if (msg != null) {
msg.put("id", id);
msg.put("read", c.getString(c.getColumnIndexOrThrow("read")).contains("1"));
msg.put("time", c.getString(c.getColumnIndexOrThrow("date")));
msg.put("m_id", c.getString(c.getColumnIndexOrThrow("m_id")));
msg.put("received", c.getString(c.getColumnIndexOrThrow("msg_box")).contains("1"));
msg.put("thread_id", c.getString(c.getColumnIndexOrThrow("thread_id")));
array.put(msg);
}
} catch (JSONException j) {
j.printStackTrace();
}
} while (c.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
}
private JSONObject populateFromMmsPart(String msgId) {
ContentResolver contentResolver = context.getContentResolver();
Uri messages = Uri.parse("content://mms/part");
Cursor c = contentResolver.query(messages, null, "_id=" + msgId, null, null);
JSONObject msg = null;
if (c.moveToFirst()) {
try {
String mid = c.getString(c.getColumnIndex("mid"));
String type = c.getString(c.getColumnIndex("ct"));
if (!"application/smil".equals(type)) {
// Put all the addresses in one place
msg = new JSONObject();
msg.put("address", getAddressesForMmsMessages(mid));
msg.put("mid", mid);
msg.put("type", type);
if ("text/plain".equals(type)) {
// MMS is just plain text, so get the text
String data = c.getString(c.getColumnIndex("_data"));
String body;
if (data != null) {
body = getMmsText(msgId);
} else {
body = c.getString(c.getColumnIndex("text"));
}
msg.put("msg", body);
}
}
} catch (JSONException j) {
j.printStackTrace();
}
}
c.close();
return msg;
}
之前我在做:
ContentResolver contentResolver = context.getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, "thread_id=109", null, null);
我收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
在 populateFromMmsPart()
方法中,您正在查询 part
table 中的记录,其 _id
与 mms
table。但是,part
table 中的 _id
是部件的 ID,而不是消息。消息 ID 在 mid
列中。当消息 ID 恰好与旧消息的部分 ID 匹配时,这显然会导致混淆。
按如下方式更改您的查询。
Cursor c = contentResolver.query(messages, null, "mid=" + msgId, null, null);
此外,在 populateFromMmsPart()
方法中,您只处理返回的 Cursor
的第一行。 Cursor
将(至少)有三行。一个是 application/smil
,一个是 text/plain
,另一个是附件的 MIME 类型。您需要遍历 Cursor
来获取它们。正如您现在所拥有的那样,您只是调用 c.moveToFirst()
,并处理那一行。