我正在编写一个需要用户当前位置的 Android 应用程序。我使用以下代码从网络和 GPS 注册位置更新(locationManager 已定义):
// Register the listener with the Location Manager to receive location updates.
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
getResources().getInteger(R.integer.gps_min_time),
getResources().getInteger(R.integer.gps_min_dist), this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
getResources().getInteger(R.integer.gps_min_time),
getResources().getInteger(R.integer.gps_min_dist), this);
我目前onCreate
在.onPause
onResume
当应用程序启动时,它会添加两个侦听器两次,一次在 中onCreate
,一次在 中onResume
。我对此有两个问题:
- 每个监听器添加两次是否意味着它实际上被添加了两次,或者第二次调用没有效果?
- 我应该
requestLocationUpdate
从中删除 sonCreate
并将它们放入,还是应该在再次添加之前先onResume
删除所有侦听器?onResume