Pardon me if m asking some discussion based questions. I am completely new to android environment and I am developing an android based application wherein i need to apply some modification in messaging system for my project. therein i need to employ a timer facility for the outgoing messages where user could be able to time his message so that at that scheduled time, the sms should go automatically. I want to know which messaging API will help me in implementing so. I can sense that dis is not upto the mark question to be asked but i searched on every site but all work in vain. Hoping to get a positive reply from your side.
You should have a look at ScheduledExecutorService
i think this is what you are searching for. This service is able to execute tasks based on TimeUnits in the background.
You might also want to check out AlarmManager. You can use it to send a custom Intent at some time in the future. It might be better than other kinds of timers (depending on your situation), because it will cause the phone to wake up when the intent is delivered. Otherwise, your code might not run if the CPU is asleep.
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'm currently making an app in Android that is checking an API which returns two things. Some text and a colour.
However I want this to be checked for updates every 15 minutes in the background and check every 5 seconds when the app is open. When running in the background it should give a notification if the status is changed.
Now I have checked numerous stackoverflow q&a's and forums, docs etc.. But I can't seem to find a good baseline for what I need. So many documentation that contradicts eachother.. I think that I need an Alarm Manager or a Service... but what do you guys suggest for my problem? The app may not harm the battery too much.
What I really would like to have is that the application doesn't have to "poll" the server every 15 minutes but that the application gets interrupted like.. "hey, there is a new status update". I can't imagine that messaging apps are constantly polling a server for updates? I haven't found much information about that topic... Any help is appreciated. Not asking for code but directions to get where I want to go.
Many thanks
If you're looking to poll the server every X seconds/minutes, AlarmManager(android guide, tutorial) is exactly what you need. However, as you point out this is probably not the best way to go about things. While the app is open you may want to look in to passing messages between the device and server via an open Web Socket. Once your app is closed you could, instead of the app polling the server, have the server push a notification, via GCM or some such, to the app when an update is available.
If you are doing both the server side project and the mobile application, You can use Any messaging service rather than polling for the server, Because there has to be a pusher implementation from the server side to push the status to the MS.
For now GMS is free, I hope it will remain the same :). Otherwise, You can use AlarmManager and IntentService to achieve your goal.
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 have an Android app which needs to poll a web server at 2 second intervals.
So I would like to know what the best way to do this would be. My first thought was to use an AlarmManager but I believe this is no good for anything more frequent than about 5 minute intervals. I have also considered using a service but I am concerned this will drain the battery. Are there any options I haven't considered? What is the best way to poll a server very frequently without killing the battery?
I also know that GCM is the ideal way to sync with a server but unfortunately it is not an option at this time.
Edit: ok, it seems from your comments that it is as I feared and there's no good solution for this. I will probably implement it in a service and press for a push mechanism instead. Thanks for your help.
The solution I have come to is gaining agreement to redesign so that the server pushes to the app only when the data has changed (it won't change that often, realistically).
In the meantime I have made some small changes to the service so that it cancels all requests when the app isn't in the foreground and reduced the poll time to 5 seconds (better than nothing, right).
I am planning to implement a chat application in Android, and need to make a few design decisions related to polling the server for updates:
How often should I poll for new messages? Will polling every minute be a good choice?
How can real time chat be supported? Should the polling be done every 5 seconds in case the user sends message and then return to long polling interval in case no new message is received?
I also need to make sure the application does not drain the battery quickly. I need to design the application on similar lines as WhatsApp. I am not sure how does it manage polling, but I know its not a battery killer.
You should look into XMPP.
WhatsApp uses a modified version of XMPP.
Here's a tutorial to get started.
Polling is not the answer for this type of application. If your application is solely Android base you should look at the Google Cloud Messaging Framework (http://developer.android.com/google/gcm/index.html).
This allows you to push messages to individual clients over xmpp (or http ping to pull). This way they can get updates almost instantly.
For the chat I'd make two different kind of polling.
First if the application is in background (every minute or something like this) and if the app is started every second or maybe long polling: http://en.wikipedia.org/wiki/Push_technology#Long_polling
For avoiding to be a battery killer, you should stop the loop for polling when screen off, or make the interval to be very long when screen off
Maybe you can keep a long lived connection to make a real time chat.
Using GCM is a better choice