启动应用程序时获取新短信的最快/最快方法-Android

Mik*_*ant 5 sqlite sms android cursor

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

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

目前我对每个短信都执行插入或忽略操作,如下所示:

insert or ignore into sms (smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTIMESTAMP, smsTYPE, smsSHORTMSG) values (?,?,?,?,?,?,?,?);
Run Code Online (Sandbox Code Playgroud)

我在启动画面活动中加载收件箱并在单独的asynctasks中发送消息,这大约需要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;
    } 
Run Code Online (Sandbox Code Playgroud)

有创意的想法吗?

Dhe*_*.S. 1

如何在单独的服务中注册一个ContentObserver以监听 Android SMS 提供程序中的更改?我不确定它是否会通知观察者有关变化的信息。但如果是这样,您的数据库将始终保持同步。但不要仅仅依赖于此,因为服务可能随时因各种原因终止。

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