0

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();
}
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
kencoder
  • 1
  • 2
  • What variable exactly ? whatever variable you use inside run you can make it global so when next time your run block runs it will get updated value . Your question is not clear enough right now . – ADM Dec 10 '21 at 11:59
  • My current project is about displaying the location (latitude and longitude) in a textview. So i want to get the latest location that it request from location manager. Because when im using postDelayed it only displays the initial longitude and latitude that it gets. – kencoder Dec 10 '21 at 12:18
  • First of you do not need to use handler here . you can setup the LocationProvider to provide location in certain intervals . So with this way you show location as you gets it . if you need to use handler just have a global variable and update it whenever you get location update and use it inside run method . – ADM Dec 10 '21 at 12:22
  • sorry kindly check it out again – kencoder Dec 10 '21 at 12:27

0 Answers0