Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster/Fastest way to get new sms messages when starting app - Android

I have an sqlite database that contains all the sms messages of the user, however when you start the app I need to get all the sms's that I might not have if the user decides to use a different sms app inbetween.

I'm looking for the best way to sync my database with the base android content provider.

at the moment I do an Insert or Ignore for each sms like so :

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

I load the inbox and sent messages in separate asynctasks in my splash screen activity this takes about 10s or so.

my 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;
    } 

Ideas for impovement?

like image 296
Mike Bryant Avatar asked Oct 22 '22 21:10

Mike Bryant


1 Answers

How about registering a ContentObserver in a separate Service to listen to changes in the Android SMS provider? I'm not sure if it notifies observers on changes. But if it does, your database will always be in sync. But don't depend solely on this, as the service may be terminated at any time due to various reasons.

For the actual sync, ensure that the "_id" column in your table is the PRIMARY KEY. Query the content provider sorted by "_id". Query your own table sorted by "_id" and read all ids. Now iterate through the two sorted lists, inserting the items missing in yours and deleting the ones missing the content provider. You do want to also remove messages that were deleted using the other SMS app, don't you? The "insert or ignore" statement won't delete the missing rows for you.

like image 73
Dheeraj Vepakomma Avatar answered Oct 24 '22 16:10

Dheeraj Vepakomma