1

I have a task in which the app detects LatLng using LocationListener and adds to ArrayList every two minutes. If the ArrayList size reaches six it sends the contents to SMS and clear the items of the ArrayList. It only detects the LatLng if the device moves only. If the mobile stops before adding six elements to ArrayList then the it was incomplete. I have used the following code to send SMS if the ArrayList size reaches six.

 private void sendLog() {

    Toast.makeText(MainPage.this,"Sending Log",Toast.LENGTH_LONG).show();
    final SharedPreferences account=getSharedPreferences("admins",MODE_PRIVATE);
    String interval=account.getString("lti", "");
    int timeInterval=Integer.parseInt(interval);

    final List<String> loglist = new ArrayList<String>();
    LocationManager logManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    logManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval*60000, 250, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            double latitude=location.getLatitude();
            double longitude=location.getLongitude();
            DecimalFormat dFormat = new DecimalFormat("#.####");
            Date date=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("kk:mm");
            SimpleDateFormat sdf1=new SimpleDateFormat("dd-MM,yy");
            String time=sdf.format(date);
            final String dateLog=sdf1.format(date);

            loglist.add("!+" + dFormat.format(latitude) + ",+" + dFormat.format(longitude) + "," + time);

            if (loglist.size()==6) {
                log = new StringBuilder();
                for (int j = 0; j < loglist.size(); j++) {
                    log.append(loglist.get(j).toString());
                }
                SmsManager smsManager=SmsManager.getDefault();
                smsManager.sendTextMessage(logPreferences.getString("admin1", ""), null,"  "+ log.toString()+"!"+dateLog+"!", null, null);
                loglist.removeAll(loglist);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    });
}

My problem is if the contents of the ArrayList not changed in next twenty minutes I have to send the incomplete ArrayList to SMS. Now how to detect the content of the ArrayList not changed for 20 minutes. Anyone knows please help. Thanks.

Praveen Kumar
  • 547
  • 1
  • 7
  • 33
  • You should have to check at every 20 minute in background Thread that the Previous Content is not same as Current Content in ArrayList of Latlng. – Rajan Bhavsar Aug 04 '15 at 05:03

3 Answers3

0

Create a backup kind of ArrayList variable. And compare its contents with your ArrayList in every 20 minutes. Use the following to compare them.

ArrayList commonList = CollectionUtils.retainAll(backuplist,yourlist);
if (commanList.size()==yourlist){
    //Send SMS because, this block is executed when no item is changed
}

And to do this in every 20 minutes, you have to use a separate thread. Use like Handler class or use AlarmManager as described here.

Community
  • 1
  • 1
Nabin
  • 11,216
  • 8
  • 63
  • 98
0

You could use a countdown timer and a bool value.

bool isUpdated = true;

countDownTimer = new CountDownTimer(20*60*100, 20*60*100) {
    @Override public void onTick(long millisUntilFinished) {
}

    public void onFinish() {
     isUpdated = false;
   }

using:

countDownTimer.cancel();
countDownTimer.start();

To start and stop the timer when the array is changed.

Then test for the bool value when sending your sms.

if(isUpdated==true){
    // Your TODO
}
else{
    // No change.
}

Don't forget to reset your bool value when you reset the timer.

0

Why don't you simplify your code by defining the following methods:

//your class's fields
private List<String> loglist;
private Handler handler;
private Runnable sendSmsRunnable;


/**
* Call this method to initialise the timed run (after 20 minutes)
*/
private void set20MinuteTimedRunnable() {
    sendSmsRunnable = new Runnable() {
        @Override
        public void run() {
            sendSms();
        }
    };
    handler = new Handler();
    handler.postDelayed(sendSmsRunnable, 20 * 1000);
}

/**
* This will append location and if it reaches threshold
* (i.e size = 6), then it will call sendSms().
*/
private void appendLocation (String location) {
    logList.add(location);
    if(logList.size() == 6) {
        sendSms();
    }
}

/**
* sends SMS and removes the timed runnable
*/
private void sendSms () {
    handler.removeCallbacks(sendSmsRunnable);
    //send the sms here
}
Sufian
  • 6,405
  • 16
  • 66
  • 120