简体   繁体   中英

Android Check current date with previous date saved in SharedPreference

I have this method below that sends messages from users device to a server. I want to save the date i sent the messages to the server in SharedPreferences and check if new messages come in and they are current than the date in the SharedPreference then i send those messages. How do i go about it.

I updated my question with how i check the date but i realize that the method for checking the date is not working because anytime i call this method the messages already uploaded to server keep uploading but i only want them uploaded once unless a new message comes in.

private void startSync() {
    Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
    Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
    startManagingCursor(cursor1);
    String transaction = "";
    String[] columns = new String[] { "address", "person", "date", "body","type" };
    if (cursor1.getCount() > 0) {
        String count = Integer.toString(cursor1.getCount());
        while (cursor1.moveToNext()){
            String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
            String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
            Date date = new Date(cursor1.getLong(0));
            formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
            //date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
            String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
            type = cursor1.getInt(cursor1.getColumnIndex(columns[4]));
            status = 1;
            CreatedBy = userName;
            ModifiedBy = userName;

            if (address.equals("MobileMoney")){

                transaction += msg;

                long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
                long updDate = prefs.getLong("lastupdate", 0) / MILLIS_PER_DAY;
                long currdate= System.currentTimeMillis() / MILLIS_PER_DAY;
                if (!(updDate == currdate)) {
                    saveExternalTransactions(userId, type, status, transaction, formattedDate, CreatedBy, ModifiedBy);
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putLong("lastupdate", currdate);
                    editor.commit();
                }

            }
        }
    }


}

you need to replace this line

long updDate = prefs.getLong("lastupdate", 0) / MILLIS_PER_DAY;

to this :

long updDate = prefs.getLong("lastupdate", 0);

it corrects your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM