私のAndroidアプリでは、場所が以前の場所から変更されている場合にのみ、アプリケーションの開始時にユーザーの現在のGPS位置を取得する必要があります。ただし、場所が変更(ieユーザーがアプリの使用中に旅行していた場合)アプリは最初から開始されます。
ユーザーの現在位置を取得した後、位置リスナーを停止する必要があります。 removeUpdatesメソッドが機能しませんでした。
この問題で私を助けてください。前もって感謝します !
mLocManager.removeUpdates(locationListenerObject);
mLocManager = null;
緯度と経度がリスナーによってキャプチャされたときに、onLocationChange
メソッド内でこれを呼び出します。
私が助けてくれたことを願っています。
LocationListener locationManager.removeUpdates(mLocListener);
を停止した後でオブジェクトをnullにすることで、LocationListenerを停止できます。つまり、緯度と経度の取得を停止する場合はmLocListener = null;
です。
メソッドlocationManager.removeUpdates(locationListener)
を呼び出すことで、継続的な位置更新を停止できます。
次に例を示します。
LocationManager locationManager;
LocationListener locationListener;
// for location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// stop location updating
locationManager.removeUpdates(locationListener);
これを試す
locationManager = activity!!.getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
if (location != null) {
Log.e(
"LOCATION",
"Latitude " + location.latitude.toString() + " | Longitude :" + location.longitude.toString()
)
if (locationListener != null)
locationManager!!.removeUpdates(locationListener!!)
}
//Toast.makeText(getApplicationContext(),location.toString(),Toast.LENGTH_SHORT).show();
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
}
override fun onProviderEnabled(provider: String) {
}
override fun onProviderDisabled(provider: String) {
}
}
_locationManager.requestSingleUpdate(String provider, LocationListener listener, Looper looper);
_
現在の場所を一度だけ取得する必要がある場合は、代わりにこのメソッドを使用してください
_locationManager.requestLocationUpdates(String provider, long minTimeMs, float minDistanceM, LocationListener listener)
_
locationManager.removeUpdates(LocationListener listener)
を使用して更新を停止します
これが機能し、同じ問題を持つ他の人を助けることを願っています。