簡體   English   中英

將方法中的變量傳遞給onCreate方法中的另一個變量

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

我正在創建一個android應用。 當前位置是硬編碼的。經度和緯度在oncreate方法中聲明。 我已經創建了一種方法來獲取oncreate方法之外的經度和緯度。

這是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");
}

如您所見,該位置是硬編碼在latitudelatitude變量中的。

我還創建了一種獲取用戶電話的最后已知坐標的方法。 這超出了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();

        }
    }
}

如何在oncreate方法中將location方法中的lattilongi值分配給longitudelatitude

如何在oncreate方法中將經定位方法中的latti和longi值分配給經度和緯度。

我建議您將緯度和經度聲明為全局變量(在onCreate方法中)

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

然后在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();
  }

或者,您可以將這兩個變量傳遞給getLocation方法。

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

以這種方式修改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. 最好的方法是將字段聲明為全局字段,然后將值分配給這些字段,然后在onCreate()或所需位置使用它。

  2. 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. 您可以做的是從getLocation()方法獲取Location實例,並在onCreate()獲取latti和longi

  1. 在MainActivity中聲明latti和longi。 就像我在這段代碼中所做的一樣。

2.after調用方法getLocation()其設置lattilongi值,設定的值latitudelongitude 請看代碼

請嘗試以下代碼:

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

另一個解決方案是吸氣劑和吸氣劑。 (旁注:您不需要緯度和經度,您可以只使用latti和longi)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM