Android countdown and android countdown

Source: Internet
Author: User

Android countdown and android countdown

Preface

When you open the welcome interface of iQiYi and other apps, there is a countdown control in the upper right corner. After the countdown, enter the main interface. Now let's implement this function.

Method 1

Using the java class Timer, TimerTask and android Handler

Ui welcome_activity.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                xmlns:tools="http://schemas.android.com/tools"                 android:layout_width="match_parent"                android:layout_height="match_parent">    <TextView        android:id="@+id/count_down"        android:layout_width="60dp"        android:layout_height="60dp"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginRight="16dp"        android:layout_marginTop="16dp"        android:gravity="center"        android:textSize="32sp"        android:textColor="#50000000"        android:background="@drawable/count_down_background"        tools:text="2"        /></RelativeLayout>
Package com. example. counttimer; import java. util. date; import java. util. timer; import java. util. timerTask; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. view. menu; import android. view. menuItem; import android. view. window; import android. widget. textView; public class WelcomeActivity extends Activity {private final static int COUN T = 1; private TextView countDown; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_welcome); initView () ;}// the first and third parameters of sehedule are the time of the first startup delay, and the third parameter is the interval of execution. The Unit is ms.
// Therefore, a message is sent to handler every second to update the UI.
// Three seconds later. In the second sehedule of timer, the private void initView () {countDown = (TextView) findViewById (R. id. count_down); final Timer timer = new Timer (); final long end = System. currentTimeMillis () + 1000*3; timer. schedule (new TimerTask () {@ Override public void run () {handler. sendEmptyMessage (COUNT) ;}}, 0, 1000); // The second parameter of schedule indicates that the method timer in run is run as soon as possible at this time. schedule (new TimerTask () {@ Override public void run () {Intent I = new Intent (WelcomeActivity. this, SecondActivity. class); I. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TASK); startActivity (I); finish (); timer. cancel () ;}, new Date (end) ;}private Handler handler = new Handler () {int num = 2; public void handleMessage (android. OS. message msg) {switch (msg. what) {case COUNT: countDown. setText (String. valueOf (num); num --; break; default: break ;}};};}
count_down_background.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <corners        android:radius="8dp"/>    <solid        android:color="#1e000000"/></shape>

 

Another empty activity will not be posted.

The result is as follows: Countdown to 0 to SecondActivity

 

Method 2

Use the CountDownTimer class encapsulated by android. Actually, Handler is used internally. All others are the same.

Package com. example. counttimer; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. countDownTimer; import android. view. window; import android. widget. textView; public class WelcomeActivity extends Activity {private final static int COUNT = 1; private TextView countDown; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_welcome); initView ();} private void initView () {countDown = (TextView) findViewById (R. id. count_down );
// The CountDownTimer constructor uses the first parameter to indicate the total time, and the second parameter to indicate the interval.
// This means that every xxx calls back the onTick method, and then calls back the onFinish method after xxx. CountDownTimer timer = new CountDownTimer (3200,1000) {int num = 2; @ Override public void onTick (long millisUntilFinished) {countDown. setText (String. valueOf (num); num --;} @ Override public void onFinish (){
// Call Intent I = new Intent (WelcomeActivity. this, SecondActivity. class); I. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TASK); startActivity (I); finish () ;}}; timer. start ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.