Android: simple homepage ad countdown

Reference article: A brief introduction to Timer and TimerTask
in several ways to implement timing and countdown in Android Method 2: TimerTask and Handler (improved without Timer)

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="倒计时:"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toLeftOf="@id/txtView1"
        android:text="5"
        android:textSize="30dp"/>
</RelativeLayout>
public class PrepareActivity extends AppCompatActivity{
    
    

    private int recLen = 5;
    Timer timer = new Timer();
    private TextView txtView;
    @Override
    public void onCreate(Bundle savedInstanceState){
    
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_prepare);

            txtView =findViewById(R.id.txtView);


            prepare();
    }

    private Handler handler = new Handler(Looper.myLooper()){
    
    
        @Override
        public void handleMessage(@NonNull Message msg){
    
    
            switch(msg.what){
    
    
                case 1:
                    txtView.setText(""+recLen);
                    if (--recLen<0){
    
    
                        timer.cancel();
                        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
            }
        }
    };

    public void prepare(){
    
    
        TimerTask task = new TimerTask() {
    
    
            @Override
            public void run() {
    
    
                recLen--;
                Message message = new Message();
                message.what = 1;
                handler.sendEmptyMessage(1);
            }
        };
        timer.schedule(task,1000,1000);
    }

}
<!-- 将广告页面注册为程序首页 -->
        <activity
            android:name=".PrepareActivity"
            android:label="@string/title_activity_prepare"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter></activity>
        <activity android:name=".MainActivity">
        </activity>

Insert picture description here

Guess you like

Origin blog.csdn.net/drawababy/article/details/112177996