更快/最快的方式来启动应用程序时获取新的短信 – Android

我有一个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应用程序删除的邮件,不是吗? “插入或忽略”语句不会为您删除丢失的行.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能