我曾试图在另一篇文章中解决这个问题但是没有成功。这就是我想要完成的:当用户单击gpsbtn时,它将从gps.printLocation方法中获取以获取long / lat。我想自动填充edittext但我想我可以通过快速谷歌搜索找出如何做到这一点。因此,如果我能将它存储在一个变量中,那就是我想要的变量,但是我的大脑不起作用。完整代码:
public class Location extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Full Screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.location);
// Go button
Button gobtn = (Button) findViewById(R.id.gobtn);
final EditText Zipcodeinput = (EditText) findViewById(R.id.Zipcode);
gobtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Edit text validation field
Pattern pattern = Pattern.compile("[0-9][0-9][0-9][0-9][0-9]"); // string needs 5 digits
// argument to matcher is string to validate
Matcher matcher = pattern.matcher(Zipcodeinput.getText()
.toString());
if (!matcher.matches()) // on Success
{
// invalid
// pop up ERROR message
Toast.makeText(getBaseContext(), " Invalid Input ",
Toast.LENGTH_LONG).show();
} else {
// validation successful
// Change to Listings Page
Intent filterIntent = new Intent(view.getContext(),
Listings.class);
startActivityForResult(filterIntent, 0);
}
}
});
// button to get to list view
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);
}
});
}
}
我想从中获取信息的类:
public class gps extends Activity implements LocationListener {
//private static final String[] S = { "Out of Service",
//"Temporarily Unavailable", "Available" };
private TextView output2;
private LocationManager locationManager;
private String bestProvider;
double latitude,longitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
try {
// Get the output UI
//output = (TextView) findViewById(R.id.output);
output2 = (TextView) findViewById(R.id.output2);
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// List all providers:
List<String> providers = locationManager.getAllProviders();
for (String provider : providers) {
printProvider(provider);
}
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
//output.append("\n\nBEST Provider:\n");
printProvider(bestProvider);
//output.append("\n\nLocations (starting with last known):");
Location location = locationManager.getLastKnownLocation(bestProvider);
printLocation(location);
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), "Error Getting GPS" , Toast.LENGTH_LONG);
}
}
/** Register for the updates when Activity is in foreground */
@Override
protected void onResume() {
super.onResume();
}
/** Stop the updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
public void onLocationChanged(Location location) {
printLocation(location);
}
public void onProviderDisabled(String provider) {
// let okProvider be bestProvider
// re-register for updates
}
public void onProviderEnabled(String provider) {
// is provider better than bestProvider?
// is yes, bestProvider = provider
}
public void onStatusChanged(String provider, int status, Bundle extras) {
//output.append("\n\nProvider Status Changed: " + provider + ", Status="
// + S[status] + ", Extras=" + extras);
}
private void printProvider(String provider) {
//LocationProvider info = locationManager.getProvider(provider);
//output.append(info.toString() + "\n\n");
}
//PRINTS the long and lat. Take out round before release
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();
}
我迷路了!谢谢!
答案 0 :(得分:3)
您可以将坐标保存到应用程序中所有类可用的共享首选项。 Shared Prefs
或者您可以创建一个“全局”类,它从Application类扩展并编写您自己的setter和getter方法来访问存储在此Global类中的数据。
public class MyApp extends Application {
public String string1;
public String getState(){
return string1;
}
public void setState(input){
this.string1 = input;
}
}
要从应用程序中的任何其他类获取从任何类使用的数据:
MyApp appState = ((MyApp)getApplicationContext());
String x = appState.getState();
appState.setState(x);
答案 1 :(得分:1)
未经测试,但我想到使用共享偏好设置:
public static final String USER_LATITUDE = "user.lat";
public static final String USER_LONGTITUDE = "user.long";
public static final String USER_SHARED_PREFS = "user.prefs";
private void printLocation(Location location) {
SharedPreferences prefs = getSharedPreferences(USER_SHARED_PREFS, MODE_PRIVATE);
Editor editor = prefs.edit();
long latitude = Math.round(location.getLatitude());
long longitude = Math.round(location.getLongitude());
editor.putLong(USER_LATITUDE, latitude).commit();
editor.putLong(USER_LONGTITUDE, longitude).commit();
}
private void doSTuff(){
SharedPreferences prefs = getSharedPreferences(USER_SHARED_PREFS, MODE_PRIVATE);
prefs.getLong(USER_LATITUDE, 0);
prefs.getLong(USER_LONGTITUDE, 0);
}
共享首选项实例是原子的,因此只要在编辑器上执行提交,读取器类中的sharedPreferences实例就可以读取更改。
可能的问题,位置的默认值,sharedPreferences中没有可用的位置对象只有long和lat意味着您无法获得位置读取的准确性或时间。
答案 2 :(得分:0)
在MainActivity中,在公共类MainActivity下扩展AppCompatActivity {:
public class MainActivity extends AppCompatActivity {
public static String test = "OK it's working";
...
在另一个活动中写:
public class NameOfYourClass extends AppCompatActivity {
String testInAnotherClass = MainActivity.test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_suppported_apps);
Toast.makeText(getApplicationContext(),testInAnotherClass , Toast.LENGTH_SHORT).show();
...