Changing variable inside Runnable postDelayed method

67 Views Asked by At

Is it possible to make a runnable postDelayed method changes the value of its variable when running? Because it only loops the initial longitude and latitude that it gets even though the location is changing. So for example there is a new location, the run method should also get that not repeating only the initial value that it gets. Here is the code below.

@Override
public void onLocationChanged(@NonNull Location location) {

    latLng = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.addMarker(new MarkerOptions().position(latLng).title("My Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    phoneNumberInput = (EditText) findViewById(R.id.phoneNumberInput);
    startButton = (Button) findViewById(R.id.startButton);
    stopButton = (Button) findViewById(R.id.stopButton);
    
    stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onDestroy();
            Toast.makeText(MapsActivity.this, "Location logging stopped!", Toast.LENGTH_SHORT).show();
        }
    });
 
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MapsActivity.this, "Location logging started!", Toast.LENGTH_SHORT).show();
            mToastRunnable.run();
        }

        private Runnable mToastRunnable = new Runnable() {
            @Override
            public void run() {
                delayInput = (EditText) findViewById(R.id.delayInput);
                nameInput = (EditText) findViewById(R.id.nameInput);
                messageInput = (EditText) findViewById(R.id.messageInput);
                String messageText = messageInput.getText().toString();
                String nameText = nameInput.getText().toString();
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                String dateTime = simpleDateFormat.format(calendar.getTime());
                String myLatitude = String.valueOf(location.getLatitude());
                String myLongitude = String.valueOf(location.getLongitude());
            
                String message = "Origin: " + nameText + "\nReason: " + messageText + "\n\nLatitude = " + myLatitude + "\nLongitude = " + myLongitude + "\n\nLink: https://maps.google.com/?q=" + myLatitude + "," + myLongitude + "\n\n Sent on: " + dateTime;
            
                SmsManager smsManager = SmsManager.getDefault();
                String phoneNumber = phoneNumberInput.getText().toString();
 
                if (phoneNumber.length() == 0) {
                    phoneNumberInput.requestFocus();
                    phoneNumberInput.setError("Field cannot be empty!");
                } else if (phoneNumber.length() < 11) {
                    phoneNumberInput.requestFocus();
                    phoneNumberInput.setError("Phone Number is invalid!");
                } else {
                    String delayNum = delayInput.getText().toString();
    
                    if (delayNum.length() == 0) {
                        delayInput.requestFocus();
                        delayInput.setError("Missing!");
                    } else {
                         int delay = Integer.parseInt(delayNum);
                         smsManager.sendTextMessage(phoneNumber, null, message, null, null);
                         mHandler.postDelayed(mToastRunnable, delay * 1000);
                    }
                }
            }
        };
    });
}


};
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
} catch (SecurityException e) {
     e.printStackTrace();
}
}
0

There are 0 best solutions below