Separate thread for CountDownTimer in Android Service?

I need a piece of advice concerning the architecture of Android app. Required functionality: 1) start timer for a todo-task, 2) tick in background, 3) update progress bar in UI (in two different Activities) - how much time left; 4) when initial time limit is exceeded (/CountDownTimer finished), user gets an alert, but time tracking goes on, 5) timer value is being saved all over the process, in case Android or user kills the app, time tracking should keep on.
I came up with two ways for doing that.
1) I start Service from my TimerActivity, there's a separate Thread with WakeLock inside the service, and a BroadcastReceiver to send intents with millisUntilFinished to my Activity in order to update UI. Separate thread launches either CountDownTimer or... I don't know what to track the time till users clicks "Done".
2) Realise both CountDownTimer and ProgressBar in TimerActivity and use some Service only to save the start time and to send an alert N millis later. That seems easier but still not clear. Is there any native "stop-watch" for this purpose?
What I have already tried:
- IntentService. CountDownTimer doesnt't work in it, it ticks only once. (no contradiction with Google documentation, frankly speaking)
- Service without any special threads inside. It works but can easily be killed as the app process goes to background or whatever.
- Service with Thread and Handler: it looked logically but didn't work neither.

To be more precise, here's my current code:


public class TimerActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timer); Intent intent = getIntent(); timerIntent = new Intent(getApplicationContext(), MainActivity.class); parcel = intent.getParcelableExtra("parcel"); long fullLength = (long) parcel.getTaskLength() * 60000; timerValue = parcel.getTimerValue(); chronometer = (TextView) findViewById(; } private final View.OnClickListener timerOCL = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case startService(new Intent(getApplicationContext(), TimerService.class) .putExtra("fullLength", fullLength) ); break; case try { unregisterReceiver(receiver); } catch (Exception e) { e.printStackTrace(); } stopService(new Intent(getApplicationContext(), TimerService.class)); Toast.makeText(getApplicationContext(), "It's done with the result " + timerValue, Toast.LENGTH_LONG).show(); break; } } }; private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateTickTackClock(intent); } }; }


public class TimerService extends Service { private final static String TAG = "BroadcastService"; static final String COUNTDOWN_BR = "countdown_br"; private final Intent bi = new Intent(COUNTDOWN_BR); private CountDownTimer cdt = null; public TimerService () { super(); } @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { cdt.cancel(); Log.i(TAG, "Timer cancelled"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { long fullLength = (long)intent.getExtras().get("fullLength"); cdt = new CountDownTimer(fullLength, 1000) { @Override public void onTick(long millisUntilFinished) { bi.putExtra("countdown", millisUntilFinished); sendBroadcast(bi); } @Override public void onFinish() { } }; cdt.start(); return START_STICKY; } @Override public IBinder onBind(Intent arg0) { return null; } }

Category:android Views:16 Time:2018-12-22

Related post

  • Separate Threads for UI and Logic - Android 2012-01-25

    I want to create separate threads for implementing the core logic and updating the GUI. Both threads should not share data with them directly. For this, i want to create a vector queue with synchronized get() and put() methods. Suppose if an onClick

  • Is it possible to use separate threads for reading and writing with Boost.Asio? 2009-12-03

    According to the Boost Documentation, having multiple threads call io_service::run() sets up a pool of threads that the IO service can use for performing asynchronous tasks. It explicitly states that all threads that have joined the pool are consider

  • Separate threads for input and output in Java TCP Server 2011-03-26

    I've been tasked with writing a tcp server and client in Java. I'm having some trouble working out the general structure of my server. The server must accept multiple connections but there is no request response protocol. Once a connection is made, t

  • suggestions for separate threads for socket input and output 2011-12-03

    I'm trying to do some socket programming in java. what i have is, i've written this program to create a server and client on my local machine and make separate two threads for socket input and out put in file. Similarly two separate threads f

  • separate thread for every connection? 2012-02-27

    I'm currently developing a twisted project which establishes a connection to another source (basic.lineReceiver) for each client connecting to the server. the response chain should look like this: client connects -> client messages id to server -

  • Using separate threads for looping emphasis on Android 2011-04-30

    This is a general topic but the specifics that led to this question are: I was looking for a way to make a Android Toast message persist longer (as others have, judging from StackOverflow questions. One of the hacks I found, from here http://thinkand

  • How to assign separate thread for each File System Watcher? 2010-02-23

    I am developing a database file system.It includes a multi-directory watcher which is a windows service and which uses the file system watcher class from .net. I want to run each watcher class on separate thread.Thread can not be extended in .net bec

  • Writing on a separate thread for a web app 2009-03-08

    For one of the URLs that my clients will be calling, I want it to return as soon as possible with minimal disruption, so even if the database is down or slow, the request still returns pretty fast. I still need to do some processing of the data sent,

  • Separate threads for socket input and output 2009-11-17

    I got assigned to work on some performance and random crashing issues of a multi-threaded java server. Even though threads and thread-safety are not really new topics for me, I found out designing a new multi-threaded application is probably half as

  • separate threads for data acquisition and plotting 2011-10-19

    I have a program written in C and running on Linux which acquires streaming data from a serial port device every 16 or so ms. This is a time critical piece of code that works fine. Another piece of code plots this data, also in real time, but its tim

  • Running separate thread for polling the device and send a keypress event to flash 2011-12-05

    I have a windows application in which I have to continuously listen to a device which has buttons on the top and when any of these buttons are pressed I need to send a keypress event to flash. For implementing this I have following questions:- • What

  • Separate thread for Java game logic 2010-09-06

    When I have made simple single thread games I implemented the game logic in much the same way that the increasingly famous Space Invaders tutorial does as shown below: public static void main(String[] args) { //This is the COMPLETE main method Game g

  • Separate thread for SQL DB Queries 2011-06-30

    I am writing an application which will have to regularly poll an SQL Database to get the latest entries. These entries will be translated into Java Objects and passed to the GUI which will then represent them graphically. I have a DatabaseManager cla

  • Creating a new thread for UI in Android 2011-06-24

    I created an Activity, after that I created a thread which reads from the phone book, do some string manipulation and based on the rules, creates various buttons on the main UI. I am having the code below but some how the application terminates. Can

  • Producing content indefinitely in a separate thread for all connections? 2012-01-17

    I have a Twisted project which seeks to essentially rebroadcast collected data over TCP in JSON. I essentially have a USB library which I need to subscribe to and synchronously read in a while loop indefinitely like so: while True: for line in usbDev

  • Getting GL10 instance on separate thread on Android 2011-01-17

    I'm making a game and I've created a separate thread for loading assets, 3d models, etc. So that the UI thread won't lock up while its loading. However, the thread for loading requires an instance of GL10 in order to load and map textures correctly.

  • how to stop infinite loop threads from android service? 2011-10-01

    i am working with threads in sample application.In my application i have used 3 threads are running in infinite loop.These 3 threads are used in android service class.when i am starting these threads then the threads are running and UI is not allowin

  • Can i use all volley network operations in a separate thread in android? 2015-03-17

    Can i use volley network operations in a separate thread for every network call like this :- new Thread() { @Override public void run() { // Creating volley request obj JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JS

  • What are the different ways for calling my method on separate thread? 2011-11-28

    I have some data calculation method (let it be "myMethod:"), and I want to move the call to another thread because I don't want to block my main UI functionality. So, started to do some research on how to call my method on another thread. As far as I

  • How to setup a separate Orchestration for Web Services only? 2012-01-11

    I have created an Orchestration that has declared a web service for Managing Sales Orders. This works fine, However I want to move the Web service out and into it's own orchestration and be able to call the orchestration when needed by other orchestr

  • File writing with overlapped IO vs file writing in a separate thread 2010-06-16

    Is there any advantage in using file writing with overlapped IO in Windows, vs just doing the file writing in a separate thread that I create? [Edit - please note that I'm doing the file writes without system caching, ie I use the FILE_FLAG_NO_BUFFER

  • Android Service makes application Hangs 2011-08-17

    Here is my Issue: In my app there is a scenario like app automatically detects the ultra sound which intern contain some information in it.And this sound should be played in Retail shope while user enter into shop my app should detect the sound which

  • Does a java agent run in a separate thread? 2010-06-18

    I feel that this is something I should know, but does a java agent (specified with -javaagent) run in a separate thread? I've read that a Java agent is a pluggable library that runs embedded in a JVM and intercepts the classloading process, but I wan

  • Is it Ok to use a Thread for each Animation in a Java program? 2010-08-31

    I am working on a Java program which contains a lot of animations as part of the UI. Each animation requires a repaint method to be called in order to make the animation happen. I was just wondering if it was good programing to use a separate thread

  • How do I save files to hard disk in a separate thread? 2011-02-01

    Hi Ive a camera and Im reading the images in real time into an array. Im applying some algortihm to the image and displaying it. Then I get the next image and display it as well. So Im streaming images from the camera to the display. However I also w

Copyright (C), All Rights Reserved.

processed in 0.103 (s). 11 q(s)