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



我是新的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插槽中放置什么?我请求帮助,但很少解释它是如何工作的。

你正在你的活动MainActivity中实现LocationListener。因此,对并发位置更新的调用将如下所示:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

确保你要实现的LocationListener来自google api,也就是导入这个:

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

而不是this:

import android.location.LocationListener;

,它应该工作得很好。

同样重要的是,在您这样做之前,LocationClient确实已连接。我建议你不要在onCreate或onStart方法中调用它,而是在onResume中调用它。在Google Location Api教程中有很好的解释:https://developer.android.com/training/location/index.html

我用这个:

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);
    }
}

相关内容

  • 没有找到相关文章

最新更新