How to make a countdown timer run in background in android? - android

I am revoking a timer in android using intent now if i want to put that timer in a background when user clicks on directly back button then how do i do that? and then when i see background apps running it should be working and can be brought to front.

The problem is that if you define the counter in your Activity, the counter is bound to the Activity life-cycle. In order to workaround this, you can create your counter in a Service and just visualize the value in Activity.
There are also several techniques which you can use when defining the counter inside the Activity. Using SystemClock to track the time in your Activity in onStart() and onStop() for example, you can manually calculate the difference between those two values and adjust accordingly your counter.

Related

How to implement countdown timer [re-open activity]?

How i should implement a countdown timer if i need the following usage:
User starts activity A;
In activity A he starts the timer;
User leaves activity A, timer runs in background;
User comes back to activity A;
User see current (updated) values of the timer;
I tried to use a Countdown Timer but it fails at step 5 - after returning to ativity A i cant see values in TextView (in LogCat i do) and system starts another timer (this way, every activity re-open starts one more timer). Should I use service instead or ... show me the way, please.
You can't be sure that after you leave your activity it will be still alive. System could kill it at any time.
The solutions for this use case could be:
Start timer in foreground Service. Such types of services have better chances to be alive.
When you leave your activity you could save last timer value and last timestamp and restore timer with this info after activity is recreated.
PS. If you also want to fire the alarm when timer is finished you should use AlarmManager but keep in mind restrictions from android 6+ (M).

How to create a timer runed in the background and display one second of a second dynamically

How to create a timer runed in the background and display one second of a second in the TextView dynamically in MainActivity.Whatever I exit app or leaving the MainActivity,the timer also keep on.
I thinked that I can use service and BroadcastReceiver or using Handler and Thread.But I can't solve it.
Having an always-on service to simply count up seconds is a bad idea, it'll waste battery life and could be killed at any time.
What you could do is something like this:
Make a counter that you can start/stop with a button.
On your your activity's onPause, save the System.currentTimeInMillis() along with the current count on your timer
When you resume your activity, use the current System.currentTimeInMillis() value to calculate what your timer should be displaying had it been really running all that time.
I have thinked a solution; Thread+Handler; EveryTime enter ther MainActivity
we just set the time interval = EndTime-System.currentTimeMillis();

Android how to stop timer when app go to background

I know there are ton of questions as this have been asked in stackoverflow. Most of those suggest to implement onResume and onPause to control how the app behaves when it goes to background/foreground. But I couldn't actually use that method in my case.
I have a timer that will run when my application start (or after user login). So it is not dependent on any activity in my app. What I want to do is to stop the timer (to save power resource) when app goes to background and restart it when app come back to foreground. By implementing onResume and onPause will stop and start the timer when user switch between the activities (because I implemented in all the activities), and this is not what I wanted.
Please give advice how this to be done in better way.
You can use cancel() method in onPauso() so when the app are in background you stop the timmer
In the method onResume() you launch other time your Timer.
you can see method in:
http://developer.android.com/reference/java/util/Timer.html
extend the Application class and add a public static counter on it. On every onPause() and OnResume(), increment or decrement it, when the counter is zero you app is in background.

how to show android timer timings as alerts in different activities?

I am implementing a timer in my application which checks an alarm all the time.This timer values am checking in my home page and showing alerts telling how much time left..How can i show these messages when i am in different Activities ?
One way would be to add your timer check code to a Fragment with no UI and use that Fragment in your various activities.
If your dialog box doesn't interact with your activity then you can start a TimerTask in a subclass of the Application object that checks your timers as often as you need. Then when a timer needs to go off, you can start a new activity that simply displays the alert dialog. Since you're starting it from the Application (which subclasses Context), you need to set the NEW_TASK flag and theme the activity to be a "dialog". You could even make the whole activity the dialog instead of starting the dialog in onStart()

Resume Activity by service in Android

I want to develop a program which reminds word (english-turkish). The things what I have to do below and please correct me if I'm wrong or bad way I'm using.
Create and Main.java class as an activity which includes a textview to show english word
Create another activity which shows Preferences to setup interval time to remind new words
Write some code under the Save button click(inside the Prefs.java class) to save settings to SharedPreferences
Inflate menu inside Main activity to show Preferences Activity
Create a service with MyService name.
Get interval from SharedPreferences inside the OnCreate method of MyService.
Inside the OnStart method Run a Timer according to interval and continousley connect to web service to get a new word.
Periodically bring to front Main activity(don't want to create every time from the begining, just want to resume activity) and show new word.
When pressed New Word use the service's function to connect and retrieve new word and show in TextView in Main activity
When pressed Ok set Activity to Pause mode and show Home Screen
I have some difficulties to Resume Main activity and passing new word.
Do you know a way to bring front Main activity periodically while it is in resume state?
Try to make an intent in the onResume() call with this flag: FLAG_ACTIVITY_REORDER_TO_FRONT which causes the launched activity to be brought to the front..
For more information click here.
There is no difference between starting activity and bringing them to foreground - read about activity lifecycle.
Service is ineffective if you want to use it as simple timer. Much better approach is to use AlarmManager and scheduling next activity start. Here you have an example.
Then just override Activity.onStart() method, to fill all fields you want.

Categories

Resources