I am using Network Location provider.
I need to call onLocationChanged method from my LocationListener only once per 1 hour.
Here is my code:
MyLocationListener locationListener = new MyLocationListener();
locationMangaer.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3600000, 0,locationListener);
But it doesn’t work. My onLocationChanged calling very often.
What parameters must I use?
From the
LocationManager#requestLocationUpdates()documentation:However you can use
requestSingleUpdate()with a Looper and Handler to run the updates once an hour.Addition
To start you can read more about Loopers and Handlers here.
You are using API 8 which is a good choice, but this limits which LocationManager methods we can call since most were introduced in API 9. API 8 only have these three methods:
Let’s use the first method, it is the simplest.
First, create your LocationManager and LocationListener as you normally would, but in
onLocationChanged()stop requesting more updates:Second, create a couple new class variables:
Of course, you ought to disable all of your callbacks in
onPause()and enable them again inonResume()to prevent the LocationManager from wasting resources by acquiring unused updates in the background.A more technical point:
If you are concerned about blocking the UI thread with the LocationManager, then you can use the second
requestLocationUpdates()method to supply a specific Looper from a new Thread (say a HandlerThread).