I have an app that tracks user location continously. Now, this works as follows:
I have foreground service that schedules repeating alarm via AlarmManager. Alarm Receiver in onReceive method starts service again. Inside service Im receiving current location from Locator (singleton class that gets location form GP Services). Alarm manager fires alarm once in a 12 minutes, and I want to set repeating time to 1 minute or less. But i`m aware of too frequenlty waking up device - I think it will consump battery very fast.
This foreground service works always.
Are there any other, better way to track location continously?
You can use LocationListener.
google location listner
Checkout answer on this thread: Getting location
In above answer, while creating object for LocationRequest call these two methods, setFastestInterval(long interval) and setInterval(long interval) which will fetch location after some interval.
Related
I have an operational Android app which reports users location within a background service. I want to integrate a feature which will notify the user when GPS signal has been lost.
Our current implementation to commence location updates is:
mLocationListener = LocationListener(LocationManager.GPS_PROVIDER, gpsDeviceCallback)
if (handlerThread?.isAlive == true) {
handlerThread?.quit()
}
handlerThread = HandlerThread("GpsLocationHandler")
handlerThread!!.start()
mLocationManager?.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE.toFloat(),
mLocationListener, handlerThread!!.looper)
with LOCATION_INTERVAL set to 1000 ms and LOCATION_DISTANCE set to 10m.
We get the expected onLocationChanged callbacks under normal operation. However, I would appreciate advice on how to detect a situation where there is a loss of GPS or handset cannot obtain an adequate GPS signal.
We have implemented a solution where we run a time task and determine if onLocationChanged is called during the timer period. The problem with this solution is that if the user handset is stationary during this time then no onLocationChanged callbacks will happen. So this approach will not work as a means of detecting no\inadequate GPS signal.
The user of onStatusChanged as a method of LocationListener is now deprecated so this is not an option either.
Is there some standard solution to this problem ? Perhaps some method which could be called in the case where no onLocationChanged callbacks happen when our check timer expires to test current GPS status?
Thanks!
you can check the current connected satellites and approximate the accuracy see :https://stackoverflow.com/a/10589949
I need to create an android service that:
Starts whenever the screen is on (whether it is at boot time or not)
sends a notification every 20 minutes (if the screen is on)
stops whenever the screen is off
Every tutorial I've read uses an activity, but I need this to be a service because the app is not supossed to be running other than when the user wants to change a setting. The documentation says I need an IntentService, but I cannot stop that manually and I cannot use a Service because it is a long running operation. I tried with an alarm manager but it didn't worked, I don't even bother to show you the code because I really don't understand it. I do not know how to make the service check if the screen is on or not, if I use a BroadcastReceiver it won't be inmediately processed so I am just stuck
To implement your requirement. You need 3 things such as Service, BroadcastReceiver & AlarmManager :
AlarmManager [which will fire after every 20 minutes]
Service [which will make changes like showing notification as system gets notifies after every 20 minutes for your particular msg]
BroadcastReceiver [which will check for screen on/off right from booting to shutting down]
Refer these links :
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115
I am using AlarmManager to poll user location periodically which is working fine- Now I would like to give my app users an option so they can restrict the location polling by specifying hours say 'Between 8PM to 10PM'.
Right now I am using AlarmManager.setRepeating method for scheduling but I am unable to configure my alarm service so that it runs every day but within certain hours.
I already know how to schedule a recurring task using AlarmManager at particular time of day but how to set the end time is what I am looking for.
You can't tell AlarmManager to do exactly that, but you could check whenever your service gets called to see if it's within the specified hours. If it is, then you proceed as normal, if not then you reschedule the alarm to start at the beginning of the next polling period.
I would like to register a BroadcastReceiver in the Android manifest, or in code if necessary, that would send a broadcast to my application based on time.
e.g. i would like my onReceive to be called time-dependently by the Android OS
at say a regular interval
or at a given time of day
or once after a specific amount of time has lapsed (say after 44 minutes from some call in my application)
How might that be possible without leaving my app running in the background all the time ?
See AlarmManager and android.intent.action.TIME_TICK
I want to create an application that can detect incoming calls and start my custom activity after a certain number of beeps (rings), I mean after 2 or 3 or 5 beeps (rings) my activity is triggered. How can I do it?
Thanks
I don't think you can count the number of rings the phone made since the start of the incoming call. There can't be a definitive measure of a single ring because the user can easily change the ringtone to a non-repetitive tune, for example, a song.
What you can do, however, is count the amount of time that passed since the arrival of the call. Set up a BroadcastReceiver for PHONE_STATE (you will need the corresponding permission in the manifest to receive the event). Once you receive the EXTRA_STATE_RINGING that came with the PHONE_STATE, set an alarm via the AlarmManager that will fire a Service that checks if EXTRA_STATE_OFFHOOK (broadcast when the call is picked up) has been broadcast after your waiting time. If not, then you can start your answering machine.
I have written a quick tutorial in my website on how to catch the call's arrival (when the phone rings), when the call is picked up, and when it ends.
My article about detecting incoming and outgoing calls, with the step-by-step instructions:
Detecting incoming and outgoing phone calls on Android
When you detect incoming call, you can start a timer, with interval equal to beepInterval * beepCount. And launch activity on this timer.