我有以下代码:
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// called when the listener is notified with a location update from the GPS
Log.d("Latitude", Double.toString(loc.getLatitude()));
Log.d("Longitude", Double.toString(loc.getLongitude()));
}
@Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
}
@Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
在我的 MainActivity
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
现在,我想要的只是将设备的当前位置一次接收到 MainActivity 类(获取海拔和经度变量,以便稍后在应用程序中使用)。
A. 如何在单次后停止接收位置?函数 lm.removeUpdates(listener) 只能在 MainActivity 类中调用。
B. 基本相同。如何在 MyLocationListener 类和 MainActivity 之间进行连接?
抱歉,我是 Android 和 Java 开发的新手。谢谢!