gpt4 book ai didi

android - 恢复备份消息,对话时间不正确

转载 作者:行者123 更新时间:2023-11-30 00:49:26 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,用于备份和恢复来自设备的消息/对话。它备份消息,以 xml 的形式导出文件,然后再恢复它。我面临的唯一问题是对话的日期/时间。它在恢复时更改为当前时间,但是当我打开任何对话时,时间是正确的。看看照片。

备份前:

备份后:

我用于备份的代码:

Uri uri = Uri.parse("content://sms/inbox");
//Uri uri = Uri.parse("content://mms-sms/conversations/");
ContentResolver contentResolver = getContentResolver();

final String[] projection = new String[]{"*"};

Cursor SMSL = contentResolver.query(Telephony.Sms.Inbox.CONTENT_URI, projection, null, null, null);
int msgscount = SMSL.getCount();
if (msgscount>0) {
msgs = new String[SMSL.getCount()][5];
int i = 0;
while (SMSL.moveToNext()) {
address = SMSL.getString(SMSL.getColumnIndex("address"));
body = SMSL.getString(SMSL.getColumnIndex("body"));
read = SMSL.getString(SMSL.getColumnIndex("read"));
date = SMSL.getString(SMSL.getColumnIndex("date"));
type = SMSL.getString(SMSL.getColumnIndex("type"));

msgs[i][0] = address;
msgs[i][1] = body;
msgs[i][2] = date;
msgs[i][3] = read;
msgs[i][4] = type;

Log.i("Date: ", String.valueOf(SMSL.getLong(SMSL.getColumnIndex("date"))));

i++;
}
SMSL.close();
}else{
msgs = new String[0][0];
Toast.makeText(getApplicationContext(),"No messages found!",Toast.LENGTH_LONG).show();
}

恢复代码:

                    ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://sms/inbox");
//Uri uri = Uri.parse("content://mms-sms/conversations/");

ContentValues values = new ContentValues();
for (int i = 0; i < readMsgsFromFile.length; i++) {
values.put("address",readMsgsFromFile[i][0]);
values.put("body",readMsgsFromFile[i][1]);
values.put("date",readMsgsFromFile[i][2]);
values.put("read",readMsgsFromFile[i][3]);
values.put("type",readMsgsFromFile[i][4]);

contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values);
Log.i("Restoring: ",readMsgsFromFile[i][2]);
}

最佳答案

谢谢 Mike M。我确实找到了解决方案,你是对的,只要用户收到或发送新消息,对话表就会更新,并且对话时间与该消息(无论是接收还是发送)的时间相同.但是在通过 contentresolver 查询写入消息的情况下,它不起作用并且对话时间是写入时的当前时间。所以我所做的是在消息恢复后立即在所有对话中添加一条临时消息。然后删除所有临时消息,这会将对话时间更新为上次消息时间。

            HashSet hs = new HashSet();

hs.addAll(list);
//list is the ArrayList<String> which contains the addressess of all the messages and
//through hashset we remove all the duplicates to get only the addressess once and hence we know the number of conversations and their addressess.

list.clear();
list.addAll(hs);
//Add some dummy message to each conversation
ContentValues values2 = new ContentValues();
for (int i = 0; i < list.size(); i++) {
values2.put("address",list.get(i));
values2.put("date_sent",readMsgsFromFile[0][1]);
values2.put("date",readMsgsFromFile[0][2]);
values2.put("type",readMsgsFromFile[0][3]);
values2.put("body","temp"); //this should be more unique
values2.put("read",readMsgsFromFile[0][5]);
values2.put("service_center","01010101");

contentResolver.insert(Telephony.Sms.CONTENT_URI, values2);
}

//Now deleting that message with body 'temp' from each conversation
Cursor c = contentResolver.query(Telephony.Sms.CONTENT_URI,null,null,null,null);
while (c.moveToNext()){
String body = c.getString(c.getColumnIndex("body"));
String mid = c.getString(0);
if (body.equals("temp")){
Log.i("Deleting ",mid);
getContentResolver().delete(Uri.parse(Telephony.Sms.CONTENT_URI+"/"+mid),null,null);
}
}
c.close();

“temp”这个词可以而且应该更独特,这样它就不会与实际消息混在一起。

关于android - 恢复备份消息,对话时间不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com