Android - 只获得一次位置

时间:2011-01-19 13:47:32

标签: java android location

我需要在Android应用程序上获取当前用户位置,所以我在网上阅读了一些教程和示例,但我看到在所有示例中,该位置是从“onLocationChange”中恢复的,这意味着每次位置改变时,都会执行“onLocationChange”中的代码。

我只需要在应用程序启动时获取用户位置。

感谢您的帮助!

6 个答案:

答案 0 :(得分:10)

您可以使用以下代码获取最后知道的位置。它获取位置提供程序并向后循环数组。即从GPS开始,如果没有GPS则获得网络位置。您可以在需要获取位置时调用此方法。

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break                 out the loop*/
Location l = null;

for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}

double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}

答案 1 :(得分:3)

您可以使用LocationManager执行此操作。getLastKnownLocation

答案 2 :(得分:3)

答案 3 :(得分:0)

将其放入主要活动中:


boolean bFirst = true;
function void onCreate(blabla) {
  if(bFirst) {
    //Do your stuff to get the location
    bFirst = false;
  }
}

将相同内容放在onResume();

答案 4 :(得分:0)

对于Phobos建议正常工作的getGPS()方法,您必须允许在AndroidManifest.xml中进行访问

这将消除您收到0.0

的错误

添加以下行:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />

答案 5 :(得分:0)

您可以使用此方法从所有提供商处获取最佳位置。 首先创建一个名为 FetchLocation

的类
public class FetchLocation implements LocationListener {
    private OnLocationFetchListner onLocationFetchListner;
    private LocationManager locationManager;

    public void setOnLocationFetchListner(OnLocationFetchListner fetchListner, Context context) {
        this.onLocationFetchListner = fetchListner;

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            onLocationFetchListner.OnFailed("PERMISSION DENIED");
            return;
        }
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        Log.d("TAG", "BestProvider: " + provider);
        if (provider != null) {
            locationManager.requestLocationUpdates(provider, 60 * 1000, 100, this, Looper.getMainLooper());
        } else {
            onLocationFetchListner.OnFailed("NO PROVIDERS");
        }
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        if (location != null) {
            onLocationFetchListner.OnComplete(location);
            locationManager.removeUpdates(this);
            locationManager = null;
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(@NonNull String provider) {

    }

    @Override
    public void onProviderDisabled(@NonNull String provider) {

    }
}

然后您创建一个 OnLocationFetchListner 接口!

public interface OnLocationFetchListner {
    void OnComplete(Location location);
    void OnFailed(String e);
}

然后你就可以在代码的任何地方调用这个方法了!

FetchLocation fetchLocation = new FetchLocation();
fetchLocation.setOnLocationFetchListner(new OnLocationFetchListner() {
    @Override
    public void OnComplete(Location location) {
        //do your work here
    }
}

    @Override
    public void OnFailed(String e) {
        Log.d("TAG", "OnFailed: "+e);
    }
}, context);

并且您必须将其添加到 AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />