gpt4 book ai didi

java - android:通过类传递变量?

转载 作者:行者123 更新时间:2023-11-30 11:44:41 24 4
gpt4 key购买 nike

我想知道如何通过类传递数据/变量?

类.java

public class AddItem extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new CurrentLocation();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
}

public void sendDataDetail(View v){
// This is where my HTTPPOST goes, need the location here
}

public class CurrentLocation implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
String Text = "My Current Location is: " + "Lat = " + loc.getLatitude() + "Long = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
}

基本上,我在 onCreate 中有 CurrentLocation(),然后在 sendDataDetail 中有一个 HTTPPOST 脚本。然后我有一个获取位置的类。

如何获取该位置并将其发送到 sendDataDetail?我应该采取什么方法?

请注意,我还在学习android,

提前致谢!

最佳答案

在安卓中,sharedpreferences用于在类之间传递信息。

这是我所知道的最简单的例子来解释它是如何工作的。 http://android-er.blogspot.com/2011/01/example-of-using-sharedpreferencesedito.html

关于java - android:通过类传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10661148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com