它是如何工作的 - requestLocationUpdates()+ LocationRequest / Listener

时间:2013-06-03 14:05:25

标签: android location updates locationlistener

我是新的Android编码器,我在请求更新本地化时遇到问题。

我使用http://developer.android.com/training/location/receive-location-updates.html的教程。

我的应用程序可以处理异常,正确获取纬度和长度,地理编码器可以处理显示地址。但我只询问一次位置 - 或者当位置发生变化时。我想做一些时间间隔。现在我开始实现教程中的代码,它看起来像这样:

public class MainActivity extends Activity implements 
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {

private static final int MILLISECONDS_PER_SECOND = 1000;

public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL =
          MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;

private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL =
          MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;

private TextView tvStatus;
private TextView tvLatitude;
private TextView tvLongitude;

LocationRequest mLocationRequest;
LocationClient mLocationClient;
Location mCurrentLocation;

boolean bNetworkEnabled;
boolean bGPSEnabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    tvStatus = (TextView)findViewById(R.id.tvStatus);
    tvLatitude = (TextView)findViewById(R.id.tvLatitude);
    tvLongitude = (TextView)findViewById(R.id.tvLongitude);

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationClient = new LocationClient(this, this, this);

    checkProviders();
}

因此已经实施了间隔和位置请求。但是在我之前给出的评论中,我应该使用某个地方requestLocationUpdates()(可能是onCreate()onStart()并删除onStop()上的请求),但我有问题用它。因此,Eclipse向我展示了3种方法:

requestLocationUpdates(LocationRequest request, LocationListener listener)
requestLocationUpdates(LocationRequest request, PendingIntent CallbackIntent)
requestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper)

所以我认为第一个在这个地方是最正确的。我应该在LocationListener位置放置什么?我请求帮助,但没有解释它是如何工作的。

2 个答案:

答案 0 :(得分:36)

您正在活动MainActivity中实施LocationListener。因此,对并发位置更新的调用将是这样的:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

请确保您正在实施的LocationListener来自google api,即导入此内容:

import com.google.android.gms.location.LocationListener;

而不是这个:

import android.location.LocationListener;

它应该可以正常工作。

在执行此操作之前,确实已连接了LocationClient。我建议你不要在onCreate或onStart方法中调用它,而是在onResume中调用它。在Google Location Api教程中,所有内容都得到了很好的解释:https://developer.android.com/training/location/index.html

答案 1 :(得分:11)

我用这个:

LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

例如,使用1s间隔:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);

时间以毫秒为单位,距离以米为单位。

这会自动调用:

public void onLocationChanged(Location location) {
    //Code here, location.getAccuracy(), location.getLongitude() etc...
}

我也将这些包含在脚本中,但实际上没有使用它们:

public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

简而言之:

public class GPSClass implements LocationListener {

    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
    }
}