My question is, how can I use a variable from one activity in another, so that variable can be taken (in any way) by another class or activity, without the need to do click
to send the data to the another activity, I have seen many examples but in all it is required to do click
to pass the data to the other activity, but in my code it does not work, because my code runs in the background without user interaction, example:
how would you use this variable in other .class.
public double getLatitude() {
if (loc != null) {
latitude = loc.getLatitude();
}
return latitude;
}
Friend Bruno, your answer works, but I can not implement it in this code, I tried it in another activity and it worked, I do not know why it does not work in this code, I do not know if it is because this class extends from an (AsyncTask), or I do not know if it has to be behind one (onCreate), in the other activity it works within the onCreate method, ACLARO: who receives the getter in this case is a class not an activity, I am sending the variables from an activity to a class, greetings
public class LongOperation extends AsyncTask {
@Override
protected String doInBackground(Void... params) {
try {
GMailSender sender = new GMailSender("[email protected]", "oscar1258");
sender.sendMail("This is a testing mail", "This is Body of testing mail","[email protected]",
"[email protected]") ;
} catch (Exception e) {
Log.e("error", e.getMessage(), e);
return "Email Not Sent";
}
return "Email Sent";
}
@Override
protected void onPostExecute(String result) {
Log.e("LongOperation",result+"");
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
create a GlobalClass,
public class GlobalClass extends Application {
private static Double longitude;
private static Double latitude;
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double LONGITUDE) {
longitude = LONGITUDE;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double LATITUDE) {
latitude = LATITUDE;
}
}
I do the setter with this code from the activity that contains the variables:
end GlobalClass globalVariable = (GlobalClass) getApplicationContext ();
globalVariable.setLongitude(longitude);
globalVariable.setLatitude(latitude);
and the getter from the activity it receives:
end GlobalClass globalVariable = (GlobalClass) getApplicationContext ();
final Double longitude = globalVariable.getLongitude();
final Double latitude = globalVariable.getLatitude();