简体   繁体   English

将方法中的变量传递给onCreate方法中的另一个变量

[英]Passing variables from method into another variable in the onCreate method

I'm creating an android app. 我正在创建一个android应用。 Currently, the location is hardcoded.The longitude and latitude is declared in the oncreate method. 当前位置是硬编码的。经度和纬度在oncreate方法中声明。 I have create a method to get the longitude and latitude outside the oncreate method. 我已经创建了一种方法来获取oncreate方法之外的经度和纬度。

This is the oncreate method 这是oncreate方法

@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. 如您所见,该位置是硬编码在latitudelatitude变量中的。

I have also created a method to get the last known coordinates of the user's phone. 我还创建了一种获取用户电话的最后已知坐标的方法。 This is out of the oncreate method. 这超出了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){
            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. 如何在oncreate方法中将location方法中的lattilongi值分配给longitudelatitude

How would I assign the values of latti, and longi in the location method, to longitude and latitude in the oncreate method. 如何在oncreate方法中将经定位方法中的latti和longi值分配给经度和纬度。

I would suggest you to declare latitude and longtitude as global variable (Out of onCreate method) 我建议您将纬度和经度声明为全局变量(在onCreate方法中)

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

then in getLocation function 然后在getLocation函数中

  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. 或者,您可以将这两个变量传递给getLocation方法。

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

Modify getLocation function in this way 以这种方式修改getLocation函数

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. 最好的方法是将字段声明为全局字段,然后将值分配给这些字段,然后在onCreate()或所需位置使用它。

  2. Return a pair instance from getLocation() 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() 您可以做的是从getLocation()方法获取Location实例,并在onCreate()获取latti和longi

  1. declare the latti and longi in MainActivity. 在MainActivity中声明latti和longi。 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 . 2.after调用方法getLocation()其设置lattilongi值,设定的值latitudelongitude 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) (旁注:您不需要纬度和经度,您可以只使用latti和longi)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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