I've searched through a number of the questions and answers in regards to the LocPoller from Commonsware and haven't found the answer to my problem. I have tried using the demo of the latest from github and I'm having issues. First I must admit that I modified the PERIOD in the demo so the alarm fires every 15 seconds. I realize this will be a large drain on the batter and it's just for testing. I would expect that the LocationReceiver's onReceive() would be called ever 15 seconds but that isn't happening.
Debugging things I see that the alarm is triggering and everything seems good in that it's getting to the PollerThread and calling sendBroadcast. So I assume I have something setup wrong, but I'm not sure what. Other than changing the PERIOD in the demo everything else is as is. I've tried running this on a device and emulator (Level 8). Any help in debugging this would be appreciated.
In the end the functionality I'm looking for is to have the ability to keep track of a phones location so at any point my app can get the current location. I saw this in my research and though it fit the bill perfect. If someone has a better idea of different approach I'd love to hear it.
Thanks,
Rindress
In the end the functionality I'm looking for is to have the ability to keep track of a phones location so at any point my app can get the current location. I saw this in my research and though it fit the bill perfect.
No, it is not.
If someone has a better idea of different approach I'd love to hear it.
Step #1: Call requestLocationUpdates() when you want to start receiving location updates in your app
Step #2: Use the location fixes delivered to your LocationListener, or call getLastKnownLocation(), as needed
Step #3: Call removeUpdates() when you no longer want to receive location updates in your app
Related
I know that similar questions have been posted in the past and the most current solution I have found is to use a JobScheduler + wakelock + Foreground Service as explained for example in this excellent article by Roberto Huertas (https://robertohuertas.com/2019/06/29/android_foreground_services/).
However my doubt is to know if there is a limit for this method. Does it really work that well? What if the App stays in the background for days or even weeks, will it still work?
If the answer to these last questions is no, is it possible to keep a background service on Android > 10 that can keep running for days without stopping?
EDIT 1:
I'm trying to create a real time GPS tracking app (In this case I'm using Firebase). The company and the users who use it, give their consent to be tracked all day long during their activity. This tracking can be stopped if the user disables the option inside the APP.
I have managed to keep the service running in the background using various techniques, but after a few hours Android kills it.
No, it's not. It's actually less possible than ever. Background services are now limited to 2 minutes after you exit the foreground. Foreground services will be kept around for a while, but they won't stick around forever.
The correct answer on Android is to find a way NOT to need a service running at all times. This is almost always possible, but methods differ depending on what you actually need to do, which you haven't given us any info on.
I have a small test App that with an Android GPS API map fragment. I use FusedLocationProvider. TarketSDK=29. Using Java.
As long as the app is active it works beautifully. On locationUpdates, I add a new point to the track and everything looks great and stays accurate. The goal is to track my hike, total distance and track and show it on the map. Works great.
As soon I lock my phone or loses focus, then the updates stop and I no longer get location updates.
Solution seems to be:
Background Service (discouraged)
Foreground Service
PendingIntent
I have poured over the docs, StackOverflow, all examples/tutorials I can find, developer.android.com, etc. I have downloaded examples of the latter 2 from GitHub; they seem incredibly obtuse (probably just me).
What are the dis/advantages of ForegroundService vs PendingIntent?
How about a bare-bones example illustrating the min features of each to implement location updates while your phone is locked in your pocket or some other app is active? Just the template minimum.
I need to save the locationUpdates that occur while my app is not active or phone is locked; in order to fill in Track when activity is restored to the app.
Some simple end-to-end guidance from my working app to something that will maintain locationUpdates and save the data would be great.
Ok - I have answered my question in a roundabout way.
I had been Searching on "retrieving location updates when app is not active". This lead to the various solutions of background service, foreground service, pendingIntents, etc.
I eventually found that if you just start a Foreground Service with a Notification, even if your phone is locked or you switch active apps, your App continues to receive LocationUpdates; as the Foreground Service runs in the same thread and therefore activates your app code (if I understand the reasons why correctly).
So, I started searching on just how to start a Foreground Service. As anyone knows that has tried to figure this out lately, this has changed more than a couple times over recent versions. The online docs at developer.android.com are not up to date. You will spend a lot of time wondering why things do not work following these docs.
Eventually, with just searching on how to start a foreground service, I came across this simple and straightforward (non-youtube-video - don't you just hate those things) tutorial. https://androidwave.com/foreground-service-android-example/
I just added this code to my existing Mapping code that works when the app is active, and tested with locking the phone and putting it in my pocket and switching apps and doing the same. It appears to solve the problem.
Update: Added code to count number of location updates and average accuracy of each update holding the phone in hand, screen on and app active as the baseline. Phone locked, or App not active no difference in number of updates nor accuracy. Phone locked and in pocket, no difference in number of updates, but accuracy suffered by from an average of 10m to an average of 13m; to be expected I assume whilst in the pocket.
I'm writing a reminder app for Android where users can create custom notifications that will appear at a time of the day that they specify. I realize that there is another similar question, but the only answer was to use the AlarmManager, which, according to the documentation, resets when the device is rebooted. I want the user to be able to set a notification to appear at a certain time on a certain date, meaning that it should retain the information through a reboot.
Ultimately though, the app needs to be notified that it needs to do something.
Eventually, I would also like to have the same behavior when the device reaches a certain location, if you have any thoughts on that. :)
Bear with me, I'm a bit of a novice programmer (I'm 17).
Thanks in advance.
The commentors are correct: you will need to use an AlarmManager to create your alarm and you will need a boot receiver to handle resetting your alarm after a reboot.
GeoLocation and GeoFences are pretty easy. I have an example app here: https://github.com/androidfu/GeofenceExample
You're not going to care about the mock location bits in that example unless you wish to test entering and exiting your target location, but the rest of the code should work for what you need.
Also, GeoFences do not persist a reboot either so it'll be good for you to get familiar with your on-boot receiver ;) You'll want to re-add your GeoFence after a reboot too.
I am quite new to Android programming and I wonder what the best approach would be to keep information updated while the screen is off.
The specific situation is as follows:
- I use a service that extends Service and implements SensorEventListener.
- I use the accelerometer to check if the user is sitting or standing (onSensorChanged).
- If the user have been sitting for a specified time the device vibrates and beeps.
The problem is:
- The service don't update at a frequency that makes the alarm go of i time. Sometimes is is late by a couple of minutes, sometimes it just updates as I turn the device-screen on.
- The update frequency seems to vary between devices.
My question is:
- What would be the best approach to solve the problem? Maybe a service isn't the best solution.
My intention is to keep the service running a long time in the background.
Also, I do not want to use a Wake lock because of the battery (which is an important factor in the situation).
// Victor
I mentioned this problem in this blog. I had faced this problem like you. I just found a workaround which I write but it does not work every device. Device's policy for sensors depends on manufacturer and manufacturer limits sensors on cheap phones. ( I don't know why but i guess it is about battery usage)
If you want to use sensors you should use wakelock.(If there is a solution with not using wakelocks, i want to try it)
I was read almost all article that have same question in Stackoverflow and somewhere else but those made me confuse.
my problem: I want my App toast something in certain time to the user (like alarm clock App that start ring in certain time) as an example, I want my App 2 days and 10 hour latter show a toast. but, during this period every thing maybe happen like application close, phone will restart or Etc... and the App doesn't show any thing.
my question is: How can do something in certain time in android App?
So now what is the solution? can any one help me and give me a sample code plz?
thank you in advanced,
What you want is an AlarmManager
You can also find a tutorial here. If you are scheduling a precise time, you might want to actually be conservative and wake yourself up a little early, then schedule a toast using a Timer for finer grained control.