Currently I am in process of developing my android application. My requirement is something like this.
I need to run a timer of 5mins and when timeout occurs I need to check certain things and take certain action. This need to be keep on running if the user starts the application.
In my knowledge I want to proceed like this
Create a service and start a timer in that service and do whatever needed when the timer expires. I will put one activity to interact with the service like start or stop.
I need suggestion for my approach and its pros and cons for this approach. To just run a timer do I need a service? If I run the repeated timer in a activity itself and if I press the back-button the activity will go background and will the timer also stop if I have started the timer in that activity?
I am sorry If I am not clear in my question. I have gone through the android application fundamentals and then thinking to ask this question. Please give me some idea.
There's no need for a service. You can use a Timer and schedule a TimerTask. If the task needs to touch the UI, then you should create a Handler to which you can post a Runnable.
P.S. You asked about what happens if your activity goes to the background. The Timer will keep running unless you cancel either the task or the timer itself in onPause or one of the other lifecycle methods. Whichever one you do it in, you should start the timer task in the matching start-up lifecycle method. If you are not going to cancel the Timer itself, you should create it as a daemon thread.
Related
I am in the process of making an app that will be triggered by a system broadcast and would take time input from the user, after which the app should just run a timer and do a task after the timer is over. My questions are as follows:
Do I need to use a background task for this, or is this possible to be achieved without it, because I need the timer to run even if the app is closed in the app drawer.
If I do need to use a background task, what should I use, an AsyncTask, a Service or a JobScheduler?
I understand that a BroadcastReceiver will listen to any system broadcasts, and since I have registered in the Manifest, the app will start on any such system broadcasts. However, as soon as the app is started due to the system change, I need it to popup a dialog box which takes input in the form of time (HH:MM:SS), and after that a timer begins which runs for that amount of time and as soon as the timer stops, another task is done.
I also don't want the task to be a one-up, meaning that I want it to be to done every time the system receives the system change broadcast.
For what you want to do, you basically need three things.
A BroadcastReceiver for receiving a system broadcast. This component is essential. Also, you don't have to worry about it being a one-up thing. A registered BroadcastReceiver will continue to run it's onReceive() method until the BroadcastReceiver is unregistered by you or the system, or if you intentionally place code in to block it from activating.
An Activity to display the Dialog. Technically, a BroadcastReceiver can also display a Dialog, but BroadcastReceivers are meant for short and quick tasks, so it's not a good place for this. An Activity where you show a DialogFragment is the better option because compared to a Service, an Activity is really the component meant to display a UI.
An AlarmManager for counting down the time. Rather than creating a Service yourself to handle the timer, you should use the AlarmManager with exact time to help you respond to the amount of time that passed. You can also use a JobScheduler as an alternative to AlarmManager, since both are meant for executing code at a later time. Which one you choose depends on the task you want to do later on. Personally, you should also consider the new WorkManager, which is the better option in my opinion. Depending on what you need to do, it will internally use a JobScheduler or AlarmManager, which helps get rid of the deciding process for you.
i use a timer in my app to start an async task (reading email) and put some status to the GUI - that works fine, as long as the app is not in background - then the timer seem to stop. Maybe it's normal.
Then i used the search function and read about the AlarmManager - but i want the timer only to run when my app is started.
How can i make sure the timer fires/works, no matter if the app/activity is in foreground / what would you recommend?
If I undestand well, you want a timer which runs only when the app is started, no matter whether it is in foreground or not. I see two options :
Set a timer in your activity : it can still live when activity is in background (onPaused or onStopped) but it can not interact with the GUI. So, you could store data in some way, and use it when activity is resumed.
Other option is to use Android services, that can continue to live with your application and execute tasks even in the background. As previously, you can retrieve data from them when the activity is back.
You should read the official documentation
Indeed, alarm manager is designed for planning tasks even if your application is stopped.
The main objective of my app is, when a button is pressed:
Collect data from the sensors
Store data in the database
Repeat periodically (every X seconds)
I want to have it done in background, and if the Main Activity is destroyed, the background progress to be still running. So if you delete the activity, I want it still running.
What I am doing right now, is having an AlarmManager with an Intent Service, to set the periodic recording of the Data, but whenever I destroy the Activity, as they are related, my app crashes.
I know there are different to run a background process, but none fits in mine:
Service: Depends on MainThread, so if the MainActivity is destroyed,
crashes
Intent Service: The one using right now, but as far as it is
related to AlarmManager, that is related to MainActivity, when it is
destroyed, crashes.
AsyncTask: The problem of this one, is that it is
supposed to do one task in background and it finishes, but what I
really want is to do it periodically until the user says to stop. So
it's not a matter of one time thing.
Is there any other way to have a background service?
Can any of the stated before be used for my purpose?
How do you recommend me to do it?
Thanks for your answers.
Try to start service in separate process using android:process=":servicename" in AndroidManifest.xml. If service is running in another process, it wouldn't be killed with activity.
I have tried to search many discussion threads but I am not clear on this thing. Being new I am unable to understand half of the things hence I am going to ask the direct question.
For my application when the final button (start analysis button) is clicked, I want a service to start, the analysis to be done inside the service is long almost 2-3 minutes, once the analysis has been done I want the service to automatically start a result activity page present in my main activity.
How can this task be achieved ? and I would also like to know that if the application will run in background and will not be closed by android automatically when on pause ?
Thanks
The definition of Android service is that it can run in the background, devoid of UI. So yes, it is possible. However, a Service by itself does not automatically run in the background (it runs on the main thread). Instead, what you want to do is probably something like this:
Have a service which listens for intents that say to start the operation.
When your user clicks the button, send the intent to that Service.
The service will start a new background thread, do some work for a while.
After finishing its work, the Service will start a new Activity. (Like this..)
Please take note, that it is considered extremely bad practice to start a new Activity if the user is not using your app. It's horrible if you're using a chat application and a random app starts taking over... Instead, a better idea is to stick a flag somewhere and then in the onResume() handler of your other Activity, check if the task is finished, and if so send the user to the other Activity.
I am creating an app that has a UIThread and a background thread. The background thread is basically being used as a timer - every second it sends a message to the UIThread to update the UI. When the user exits the app by hitting the backbutton, the thread continues to run. I want this to happen since the user may want to open another app while the timer continues to count down.
My question is when the user comes back to my app. I want to connect to that background thread that is running to display the current state of the app - how much time is left, etc. My question is how to hook back in to the thread that is still running in the background. I have tried using Thread and AsyncTask, but the same issue occurs.
Thanks for any help that you can provide.
Your thread is still turning by sheer chance - your application is in fact still running but it and the thread will be shut down when Android decides it needs the resources.
However what you want to do is well-provided for in Android - you need to implement a Service to have a process that runs in the background separately from your application. You can even have a Service start at boot and run whether or not your application is started.
This http://developer.android.com/reference/android/app/Service.html has most of what you need to know. To communicate between the Service and a foreground Activity you'll need to bind to a service interface, which is fortunately very easily done.
First thing that comes to mind is to change your timer thread to a Service and have apps interested in it bind to that service. Based on the Android documentation and suggested app design, you cannot depend on that thread to not be killed by the OS whenever it deems necessary.
http://developer.android.com/guide/topics/fundamentals/services.html
The android system provides a broadcast event every minute, it's call TIME_TICK.
You should:
Create a service. This is the recommended way to have a part of the app running in the background
Listen to the TIME_TICK event. This will consume less battery. (It won't wake the phone, though, so use an ALARM, too)
Add an alarm (to wake the phone if necessary)
Let the UI and the service interact. You need a callback via rpc (see the last callback example on the api page)
You should also ensure that the phone can sleep during the timeframe. You thus may want to compute the state as a delta between the starting point and now, instead of updating the state all the time.