disable GPS signal during location update
I am making location tracker application in android so I call a function locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, listener);
So开发者_Python百科 in this example i set 5minute interval. Now my question is why GPS signal is constantly activate, though my interval time is 5minute..
Can't is deactivate till 5 minute and after 5 minute can't it automatically activate?? I thing device require more battery power though it constant active... What I have to do to deactivate GPS for particular interval defined in function.
No you can't but you can always run a service that asks for a single shot fix every five minutes.
For e/g use requestSingleUpdate (String provider, PendingIntent intent)
To broadcast an intent when a single shot fix is obtained.
I had exactly the same problem with my app. I used a timer initialized on startup:
serviceHandler = new Handler(); serviceHandler.postDelayed( new RunTask(),1000L );
This runs locupdate and adds one to counter every second.
class RunTask implements Runnable {
public void run() {
++counter;
locupdate(0,0);
serviceHandler.postDelayed( this, 1000L );
}
}
this is the locupdate function used above. Note, mlocManager is defined globaly as a LocationManager.
public void locupdate(int minTime, float minDistance) {
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
if (mlocListener != null && mlocManager != null) {
mlocManager.removeUpdates(mlocListener);
}
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, mlocListener);
}
One last thing. At the end of the onLocationChanged method in LocationListener I remove updates after 5 locations fixes:
mlocManager.removeUpdates(mlocListener);
This is similar to requestSingleUpdate() but i find it more flexible. Hope this helps!
Basically this function takes time interval as follows,
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, listener);
the meaning over here is that give me GPS update if you found within the given time with no
other priority tasks.
so this is you method
public void requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent)
& these are the parameters, please read it carefully you can understand it
Parameters minTime = the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance = the minimum distance interval for notifications, in meters criteria contains parameters for the location manager to choose the appropriate provider and parameters to compute the location
intent = a {#link PendingIntent} to be sent for each location update
That is all about
Best Regards, ~Anup
精彩评论