【发布时间】:2011-06-07 13:38:27
【问题描述】:
我之前在另一篇文章中尝试过解决这个问题,但没有成功。这就是我想要完成的事情:当用户单击 gpsbtn 时,它将从 gps.printLocation 方法中获取长/纬度。我想自动填充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();
}
我很迷茫!谢谢!
【问题讨论】:
-
类似或相同的问题? stackoverflow.com/questions/6265885/…
-
有没有办法从单独的类中访问它?我想保持更有条理,让事情分开。
标签: android class android-intent global-variables