简体   繁体   中英

Passing variables from method into another variable in the onCreate method

I'm creating an android app. Currently, the location is hardcoded.The longitude and latitude is declared in the oncreate method. I have create a method to get the longitude and latitude outside the oncreate method.

This is the oncreate method

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_home );

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    getLocation();

    //Doublin Co-ordinates
    final double latitude = 53.3498;
    final double longitude = -6.2603;

    //Getting th data
    getForecast(latitude, longitude);
    Log.d(Tag,"MAIN UI code running");
}

As you can the see the location is hardcoded in the latitude and latitude variables.

I have also created a method to get the last known coordinates of the user's phone. This is out of the oncreate method.

void getLocation() {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

    } else {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null){
            double latti = location.getLatitude();
            double longi = location.getLongitude();;
        } else {
            Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();

        }
    }
}

How would I assign the values of latti , and longi in the location method, to longitude and latitude in the oncreate method.

How would I assign the values of latti, and longi in the location method, to longitude and latitude in the oncreate method.

I would suggest you to declare latitude and longtitude as global variable (Out of onCreate method)

final double latitude = 53.3498;
final double longitude = -6.2603;

then in getLocation function

  if (location != null){
      double latti = latitude ;
      double longi = longitude;
   } else {
      Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
  }

Or you can pass this two variables to getLocation method.

//Doublin Co-ordinates
final double latitude = 53.3498;
final double longitude = -6.2603;
getLocation(latitude,longitude);

Modify getLocation function in this way

void getLocation(double latitude,double longitude) {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    } else {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null){
            double latti = latitude;
            double longi = longitude;
        } else {
            Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
        }
    }
}
  1. The best way would be to declare the fields as global and then assign the value to the fields and then use it in the onCreate() or where you want.

  2. Return a pair instance from getLocation()

     onCreate() { .... Pair<Double, Double> locationInfo = getLocation(); final double latitude = locationInfo.first; final double longitude = locationInfo.second; } Pair<Double, Double> getLocation(){ .... double latti = location.getLatitude(); double longi = location.getLongitude(); return new Pair<Double, Double>(latti, longi); } 
  3. What you can do is Location instance from the getLocation() method and fetch the latti and longi in the onCreate()

  1. declare the latti and longi in MainActivity. like i did in this code.

2.after calling the method getLocation() which set the latti and longi values, set the value of latitude and longitude . please see the code

please try this code:

public class MainActivity extends Activity 

{
        double latti; //new code
        double longi; //new code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_home );

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    getLocation();

    //Doublin Co-ordinates
    final double latitude = latti; //i changed this line
    final double longitude = longi;//i changed this line

    //Getting th data
    getForecast(latitude, longitude);
    Log.d(Tag,"MAIN UI code running");
} //end of onCreate

void getLocation() {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

    } else {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null){
             latti = location.getLatitude();//i changed this line, (drooped the double)
             longi = location.getLongitude();//i changed this line, (drooped the double)
        } else {
            Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();

        }
    }
}

    } //end of MainActvity

another solution is getters & setters. (side note: you dont need the latitude and longitude, you may just use latti and longi)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM