我是新的 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
插槽中放置什么?我寻求帮助,几乎没有解释它是如何工作的。