Java 仅等待位置已更改(位置)

Java 仅等待位置已更改(位置),java,android,android-asynctask,location,google-maps-android-api-2,Java,Android,Android Asynctask,Location,Google Maps Android Api 2,我需要获得当前位置,然后-执行下一个代码。此方法已完成,我如何等待?onLocationChanged自动调用,这就是我遇到问题的原因。有人知道如何做得更正确吗? 我把它弄得很愚蠢,在OnLocationChanged()中我调用了onResume(),但这是一个非常糟糕的主意 @Override public void onLocationChanged(Location location) { final Location loc = location; Log.

我需要获得当前位置,然后-执行下一个代码。此方法已完成,我如何等待?onLocationChanged自动调用,这就是我遇到问题的原因。有人知道如何做得更正确吗? 我把它弄得很愚蠢,在
OnLocationChanged()
中我调用了
onResume()
,但这是一个非常糟糕的主意

    @Override
public void onLocationChanged(Location location) {

    final Location loc = location;

    Log.d("myLogs", "OnChange2");
    Log.d("myLogs", "2" + loc.getLatitude() + "," + loc.getLongitude());
    myLat = loc.getLatitude();
    myLong = location.getLongitude();
    onResume();

}

为此,您可能需要使用
AsyncTask
。请参阅以获取答案。基本上,在
onPreExecute
中可以启动对话框(它在调用
doInBackground
之前启动)。这意味着您正在等待找到并显示对话框的时间。然后在
doInBackground
中可以获得位置。完成后,调用
onPostExecute
。您可以从内部
onPostExecute
停止。如果需要,您可以检查位置是否不为null,然后从
onPostExecute
内部调用其他函数

这可能是一种方式。你可以从中学到一个基本的例子。您也可以从阅读文档开始,然后阅读

其他一些类似的有用问题:


希望这能有所帮助。

我知道OP已经接受了上述答案,但我感觉OP想要一个更简单的答案

我假设OP有一个带有活动的android应用程序。我是这样宣布的:

public class HelloAndroidActivity extends Activity implements LocationListener {
@Override
protected void onPause() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
    super.onPause();
}

@Override
protected void onResume() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 1000, 1, this);
    super.onResume();
}
@Override
public void onLocationChanged(Location location) {
    // Update the location fields
    ((EditText)findViewById(R.id.latField)).setText(Double.toString(location.getLatitude()));
    ((EditText)findViewById(R.id.longField)).setText(Double.toString(location.getLongitude()));
}
OP对生命周期方法如何工作以及何时应该完成工作感到困惑。我的简历和暂停方法如下所示:

public class HelloAndroidActivity extends Activity implements LocationListener {
@Override
protected void onPause() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
    super.onPause();
}

@Override
protected void onResume() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 1000, 1, this);
    super.onResume();
}
@Override
public void onLocationChanged(Location location) {
    // Update the location fields
    ((EditText)findViewById(R.id.latField)).setText(Double.toString(location.getLatitude()));
    ((EditText)findViewById(R.id.longField)).setText(Double.toString(location.getLongitude()));
}
请注意,我的onResume要求在有位置更新时通知我,而onPause方法要求不再通知我。你应该小心不要要求更新的时间间隔比你真正需要的时间间隔小,否则你会耗尽你的电池

由于活动实现LocationListener,因此我的onLocationChanged方法如下所示:

public class HelloAndroidActivity extends Activity implements LocationListener {
@Override
protected void onPause() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
    super.onPause();
}

@Override
protected void onResume() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 1000, 1, this);
    super.onResume();
}
@Override
public void onLocationChanged(Location location) {
    // Update the location fields
    ((EditText)findViewById(R.id.latField)).setText(Double.toString(location.getLatitude()));
    ((EditText)findViewById(R.id.longField)).setText(Double.toString(location.getLongitude()));
}
这只是获取新位置并更新我在活动中的一些文本编辑文本字段。我唯一需要做的另一件事是将GPS权限添加到我的清单中:

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


因此,如果我问如何开始使用位置管理器和位置服务,我会这样开始。我并不是想从被接受的答案中拿走任何东西,我只是认为在onResume和OnLocationMethod中有一个基本的误解,那就是如何改变方法

你不应该在简历上打电话。当位置发生变化时,您希望发生什么情况?我必须从那里获取坐标,并在onResume()中使用它。我想我必须学习AsyncTask,但现在有什么更简单的解决方案?我想,如果您愿意帮助您的话,我会找个时间。这是直截了当的。您可以尝试从3个基本函数开始:background、onPreExecute和onPostExecute。onPreExecute在启动后台进程之前调用。doInBackground中的内容在后台线程中运行。onPostExecute在后台进程之后运行。可以,但为1。在哪个线程onLocationChange()中运行以及它从何处开始。2如果系统正在调用此方法,如何在后台执行onLocationChange()?您可以使用
requestLocationUpdates
doInbackground
内部请求位置更新,例如:
LocationManager mlocManager=(LocationManager)getSystemService(Context.location\u服务);LocationListener mlocListener=新建MyLocationListener();mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,mlocListener)