I have few questions about getting current location of the device.
I believe only way to do that is using requestLocationUpdates(....).
if I subscribe to requestLocationUpdates... with duration set to 1 hour and after 1 hour if my phone is sleeping (or during that 1 hour I rebooted my phone), does the listener still work?
A. If yes - After 1 hour, when I get update about location, I want to change duration to 2 hours. Is there a way to do that? If not, can I call removeUpdates, and immediately call requestLocationUpdates with duration as 2 hours?
B. If No - I was planning to set an alarm to go off after 1 hour, and call removeUpdates & requestLocationUpdates in the alarm receiver. Is that right strategy.
I'm not 100% sure but If you reboot your phone service isn't started again, also as is written in a manual:
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.
This is only a hint for update time but it's not reliable.
Your practice should be to make a Service which should use Handler and postDelayed to set timed action within a runnable. I hope I helped.
Related
I'm developing an app that checks the weather based on the current location every 3 hours. I'm getting the location using the FusedLocationProviderApi and using a pendingIntent to a BroadCastReceiver that start up an IntentService.
In the FusedLocationProviderApi you can specify an interval period when you are creating the LocationRequest. So if I specify the interval to be 3 hours and the fastest interval as well to be 3 hours (I don't want to get updates before that), what happens if location is not available when it's time to do a location update?
Will I still get the location update intent at the scheduled time? I would like to use the last known location if the location is not available, but I need to be certain that I am still getting the PendingIntent at the scheduled time.
Or is it better to use an alarm manager to handle the periodic work and request the location update from within the IntentService instead?
Thanks
In the FusedLocationProviderApi you can specify an interval period when you are creating the LocationRequest. So if I specify the interval to be 3 hours and the fastest interval as well to be 3 hours (I don't want to get updates before that), what happens if location is not available when it's time to do a location update?
In this scnerio device must be awake to keep alive your location request. So it means you must have a non-stop(theoretically) background service, and partial wake lock as well. They sound not good.
Instead, you could refer AlarmManager approach which is set to wake up at each 3 hours. Then idea works like below
Device wakes up
Makes location request asap (set interval values to zero)
Continues to sleep after receiving location (and also doing your actual work)
I have an Android widget scheduled to update every hour
android:updatePeriodMillis="3600000"
However even when I change the system clock (forward one hour or more) the widget update method is not being called, no visual changes or logs happen.
I even wait a couple of minutes thinking that the OS may wait till the next minute because it doesn't need precision, but still nothing triggers.
Changing the system time won't trigger the basic widget updates?
You are doing right, but there is no guarantee it will be updated at exact time you expected. It could be delayed. Check the documentation.
The actual update is not guaranteed to occur exactly on time with this value.
The widget uses another internal clock to update itself, You'll need to wait 30 minutes in order to see the change and this cannot be hasted by changing the device's date.
If less time is required, then AlarmManager is the option.
IMPORTANT: If widget frequency is 30 minutes, ALL USERS will update the widget exactly at 00:00 and 00:30 of each hour. So if the widget consults a service the server will get peaks of requests every half an hour.
Posted question recently about why my onGpsStatusChanged is called only once and not called anymore, got no answers so will assume solution is unknown (been trying it out for 2 days), so is there perhaps any alternative solution to detect if GPS signal was lost and no location updates are available?
I am running LocationListener in background service.
I need to detect when user lost GPS -> Start alarmManager for 5 minutes and if GPS appears online again -> cancel alarm.
The only problem I have detecting if gps was lost or not.
Loosing GPS just means that you will receive no more location updates.
So why not just setting the Alarm again with each location update you receive.
The set-Methods of the AlarmManager are automatically cancelling Alarms previously set with the same PendingIntent. So you will have only one Alarm set all the time. As soon as no new location is received for longer than 5 minutes your Alarm will be executed.
May be setting the Alarm again every second is too expensive (I have no idea about that). In this case, add a counter, that resets the AlarmManager only every n-th location update. The idea is still the same.
I want to track the user location at every 1 minute and store user location in database.
If the user location does not change for 30 min then i have to give the notification to user and if user does not close app at particular time.
e.g: 8.00 pm then also i have to give notification to user
The tracking should be in separate thread so that app can perform other task while location tracking is in progress.
I am totally stuck in this that how can i make separate thread for three things:
for location tracking
for 30 min reminder notification
for 8.00 pm reminder
if the notification is shown to user and if the location changes then notification should be removed and tracking should be done normally
I see no need to use threading for this. The Android documentation on the location strategy page defines how to get updates on the location, and the Countdown timer can be used to do your timing.
You need to use Service for this, and in service you can implement the location listener with with one minute interval, and for battery optimization you should you Google Location API. And you also use the Activity recognition for finding out the user movement.
Your battery life will suck if you check the location every minute.
Anyway, all of these tasks can be completed by using the AlarmManager. You'll set up a time for the next event and when it happens your code will run, even if your Activity is in the background. I'm not exactly sure what you mean by "close the app at 8 pm", the app doesn't need to be visible for these things to happen.
I would like to write an app on Android to upload my GPS location to an external website once every ~5 minutes. This needs to have as minimal an impact on battery life as possible, but it also needs to work without any user interaction. (Background: I'm competing in an Ironman triathlon which will take me about 14 hours to complete, and want to broadcast my location in near-real-time but without having to worry about fiddling with my phone.)
So my initial thought is to write a Service which uses LocationManager.requestLocationUpdates() with a minTime of 5 minutes, but will this actually wake the device up every 5 minutes for my service to do its job?
It sounds like I would also need to use AlarmManager.setInexactRepeating() to make sure my service is awake while it completes its task but how does that play with requestLocationUpdates()? Should I instead set minTime=0 on requestLocationUpdates() but then go back to sleep as soon as the next update is obtained?
Any general guidance on how to design this is greatly appreciated. I'm a competent Java programmer & will be using Google Maps on the server to plot my location, but am pretty new to Android development so I'm basically looking for a high-level plan on how to architect the client app.
Your service must be alive all the time you want to receive updates.
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29
You can tell how often you want to be informed of location change with minTime parameter. It does not however decrease battery consumption. GPS is enabled unless you use removeUpdates method no matter how often you want to receive updates.
You can use another approache:enable GPS using method above, read one value, use removeUpdates method, wait 5 minutes and all over again. Delay between enabling and retreiving a location can be between few seconds to few minutes.