Android: call a separate class to return value
I am trying to store the value of the GPS coordinates that are in one class to another. Basically I have a button and currently I used an intent to change 开发者_如何学编程layout that displays the coordinates. I want to store the coords on the button click but I'm not sure whether you should use an intent, a run() , or anything.
The code I want to pull from is this:
public class gps extends Activity implements LocationListener {
//
// body
//
private void printLocation(Location location) {
output2.append("Lat:"+location.getLatitude()+"\nLong: "+location.getLongitude());
latitude = Math.round(location.getLatitude());
longitude = Math.round(location.getLongitude());
Toast.makeText(getBaseContext(), "Lat: " + latitude + "| Long: " + longitude, Toast.LENGTH_LONG).show();
}
where the values are store in "latitude" and "longitude"
I use this to change between screens:
Button gpsbtn = (Button) findViewById(R.id.gps); //temp use of gps button
gpsbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Toast.makeText(getApplicationContext(), "Please wait...", Toast.LENGTH_LONG).show();
Intent gpsIntent = new Intent(view.getContext(), gps.class);
startActivityForResult(gpsIntent, 0);
}
});
All I want is to store a numeric value. Thanks!
If you need to pass those values from one activity to another (you didn't specify exactly what you wanted to do with latitude and longitude) you'd add them as extras in the Intent you use either to call startActivityForResult() (if starting a new activity) or setResult() (if returning to a previous activity).
精彩评论