我有一个sqlite数据库,其中包含用户的所有短信,但是当你启动应用程序时,如果用户决定在其间使用不同的短信应用程序,我需要获取我可能没有的所有短信.
我正在寻找将我的数据库与基本android内容提供程序同步的最佳方法.
目前我为每个短信执行插入或忽略,如下所示:
insert or ignore into sms (smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTIMESTAMP, smsTYPE, smsSHORTMSG) values (?,?,?,?,?,?,?,?);
我加载收件箱并在我的启动画面活动中以单独的asynctasks发送消息
这需要大约10s左右.
我的doInBackground:
@Override
protected Void doInBackground(Void... arg0) {
int thread_id = 0;
int _id = 0;
int read;
String number;
boolean first = true;
String msg;
long timestamp;
/**
* RECUPERATION DES MESSAGES RECUS
*/
// PREPARE CURSOR
cursorInbox.moveToFirst();
while (cursorInbox.moveToNext()) {
if(first){
cursorInbox.moveToFirst();
first = false;
}
// RECUPERE THREAD_ID + ID_SMS + NUMERO DU CONTACT
thread_id = cursorInbox.getInt(cursorInbox.getColumnIndexOrThrow("thread_id"));
_id = cursorInbox.getInt(cursorInbox.getColumnIndexOrThrow("_id"));
number = cursorInbox.getString(cursorInbox.getColumnIndexOrThrow("address"));
msg = cursorInbox.getString(cursorInbox.getColumnIndexOrThrow("body"));
read = cursorInbox.getInt(cursorInbox.getColumnIndexOrThrow("read"));
timestamp = cursorInbox.getLong(cursorInbox.getColumnIndexOrThrow("date"));
// CREER LE SMS
dataManip.insert(_id, thread_id, msg, number, read, timestamp, "received",ContactsFactory.getMessageShort(msg));
i++;
publishProgress(i);
}
Log.d(TAG,"LoadActivity - Inbox Loaded");
return null;
}
推动的想法?
解决方法:
如何在单独的服务中收听registering a ContentObserver
以收听Android SMS提供商的更改?我不确定它是否会通知观察者有关变化的信息.但如果确实如此,您的数据库将始终保持同步.但不要仅仅依赖于此,因为服务可能由于各种原因随时终止.
对于实际同步,请确保表中的“_id”列是PRIMARY KEY.查询按“_id”排序的内容提供程序.查询按“_id”排序的自己的表并读取所有ID.现在遍历两个排序列表,插入您缺少的项目并删除缺少内容提供程序的项目.您还想删除使用其他SMS应用程序删除的邮件,不是吗? “插入或忽略”语句不会为您删除丢失的行.
原文地址:https://codeday.me/bug/20190629/1329809.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。