可能重复:
Android AVD wifi 错误
我正在使用 Android,我想在文本框中显示当前纬度值,在另一个文本框中显示当前经度值。我写了下面的代码,但它不起作用。它没有在相应的文本框中设置任何纬度或经度值。我的代码有问题吗?
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private static final String TAG = "CurrentLocation";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latituteField = (TextView) findViewById(R.id.lat1);
longitudeField = (TextView) findViewById(R.id.lat2);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
@Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lng);
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
或者我需要做一些事情,比如从某个地方传递纬度或经度值?还是会自动获取当前位置值?
我的清单文件中有这些-
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
还有什么我需要补充的吗?
当我在 logCAT 中看到时,我也收到了这个错误
07-18 22:43:59.836: E/Trace(3410): error opening trace file: No such file or directory (2)
我正在进入我的文本框,Provider Not Available
因为它将进入其他循环。