Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to read or sync only newly added smses ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 171
    Comment on it


    Basically we sync all the smses everytime when user do login in our app but this is time consuming task to do all the time so it would be better and effective if we sync only new smses after last sync.

    so basically for the first time we have to save some sync time on response success in do in background like this :

    if (response.contains("success")) {
    
            Utils.saveStringInSP(aContext, "smsSyncTime", "" + Utils.getCurrentTimeStamp());
            }

    Function to save sharedpreference in Utils class :

    public static void saveStringInSP(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    Fucntion to get current time stamp :

    public static Long getCurrentTimeStamp(){
        Calendar myCalendar = Calendar.getInstance();
        return myCalendar.getTimeInMillis();
    }

    so we are saving time stamp after getting success response during sync. Now wo we will check time stamp of every contacts if any sms is newly added then its obviuos that timestamp will be greater than last sync time.

    Function to get all smses and save only newly added sms based on time stamp value like this :

    SMSBean is a class that contains getter setter of variable body,read,date etc.

    public static ArrayList<SMSBean> getSMSInformation(Context mContext) {
        ArrayList<SMSBean> smsDetailsList = new ArrayList<>();
        Cursor smsCursor = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
        try {
            if (smsCursor != null && smsCursor.getCount() > 0) {
                int totalSMS = smsCursor.getCount();
                if (smsCursor.moveToFirst()) {
                    for (int i = 0; i < totalSMS; i++) {
                        SMSBean smsBean = new SMSBean();
                        smsBean.setId(smsCursor.getString(smsCursor.getColumnIndexOrThrow("_id")));
                        smsBean.setAddress(smsCursor.getString(smsCursor
                                .getColumnIndexOrThrow("address")));
                        smsBean.setMsg(smsCursor.getString(smsCursor.getColumnIndexOrThrow("body")));
                        smsBean.setReadState(smsCursor.getString(smsCursor.getColumnIndex("read")));
                        smsBean.setTime(smsCursor.getString(smsCursor.getColumnIndexOrThrow("date")));
                        if (smsCursor.getString(smsCursor.getColumnIndexOrThrow("type")).contains("1")) {
                            smsBean.setFolderName("inbox");
                        } else {
                            smsBean.setFolderName("sent");
                        }
    
                        if (Utils.getStringFromSP(mContext,"smsSyncTime") != null) {
    
                            if (compareSyncTime(Long.parseLong(Utils.getStringFromSP(mContext, "smsSyncTime")), Long.parseLong(smsBean.getTime()))) {
                                smsDetailsList.add(smsBean);
                                smsCursor.moveToNext();
                            }
                        }else{
                            smsDetailsList.add(smsBean);
                            smsCursor.moveToNext();
                        }
    
                    }
    
                } else {
                    Toast.makeText(mContext, "NO SMS Available to Read", Toast.LENGTH_SHORT).show();
                }
                smsCursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    
        return smsDetailsList;
    
    }
    
    
    public static boolean compareSyncTime(long aLastSyncTime, long aContactTimeStamp){
    
        if(aContactTimeStamp > aLastSyncTime){
            return true;
        }else{
            return false;
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: