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 Time:2018-12-31 Views:11

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

Copyright (C), All Rights Reserved.

processed in 0.500 (s). 13 q(s)