微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更快/最快的方式来启动应用程序时获取新的短信 – 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应用程序删除邮件,不是吗? “插入或忽略”语句不会为您删除丢失的行.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐