user1096087
user1096087

Reputation: 33

Countdown timer in android widget

How can I do a countdown timer in android and insert this timer in a android widget? The timer that I want to do is like this: http://www.timeanddate.com/clocks/freecountdown.html

Thank you

Upvotes: 0

Views: 8397

Answers (2)

Code Droid
Code Droid

Reputation: 10472

Another option is for you to run the Countdown Timer outside the widget, and send broadcasts which will be received and processed by the Widget after going thru a broadcast receiver. So you might consider putting the CountdownTimer in a service or other part of the application and on each tick or at end issue a broadcast which the Widget will get via a broadcast receiver. This keeps the Widget lightweight.

Upvotes: 2

Sam
Sam

Reputation: 86948

Consider using the Android CountDownTimer in any layout you please.

public class Example extends Activity {
    long mMilliseconds = 60000;
    SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");
    TextView mTextView;

    CountDownTimer mCountDownTimer = new CountDownTimer(mMilliseconds, 1000) {
        @Override
        public void onFinish() {
            mTextView.setText(mSimpleDateFormat.format(0));
        }

        public void onTick(long millisUntilFinished) {
            mTextView.setText(mSimpleDateFormat.format(millisUntilFinished));
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        mTextView = (TextView) findViewById(R.id.text);

        mCountDownTimer.start();
    }
}

Upvotes: 4

Related Questions