5

我有一个包含用户所有短信的 sqlite 数据库,但是当您启动应用程序时,如果用户决定在其间使用不同的短信应用程序,我需要获取我可能没有的所有短信。

我正在寻找将我的数据库与基本 android 内容提供程序同步的最佳方式。

目前我对每条短信进行插入或忽略,如下所示:

insert or ignore into sms (smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTIMESTAMP, smsTYPE, smsSHORTMSG) values (?,?,?,?,?,?,?,?);

我在初始屏幕活动中加载收件箱并在单独的异步任务中发送消息,这大约需要 10 秒左右。

我的 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;
    } 

改善的想法?

4

3 回答 3

1

如果您的数据库已编入索引,则删除/禁用索引:插入会更快。稍后在新线程/任务中重建索引。

解决方案2:第一步不要只在内存中(在集合中)写入数据库,稍后您将进行写作。

于 2012-12-24T17:09:43.583 回答
1

如何将收件箱中每条短信的时间戳与数据库中的最新时间戳进行比较,然后只插入时间戳较新的短信。

如果您还从收件箱中的最后一条短信开始倒退,那么您可以在收到比最新短信更早的第一条短信时立即跳出循环。

于 2012-12-27T18:40:22.230 回答
1

在单独的 Service 中注册一个ContentObserver来监听 Android SMS 提供者的变化怎么样?我不确定它是否会通知观察者发生变化。但如果是这样,您的数据库将始终保持同步。但不要只依赖于此,因为各种原因随时可能终止服务。

对于实际同步,请确保表中的“_id”列是PRIMARY KEY. 查询按“_id”排序的内容提供者。查询自己按“_id”排序的表,读取所有id。现在遍历两个排序列表,插入您的列表中缺少的项目并删除缺少内容提供者的项目。您还想删除使用其他 SMS 应用程序删除的消息,不是吗?“插入或忽略”语句不会为您删除缺失的行。

于 2012-12-28T10:56:47.140 回答