English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android Implementation of App Activity Timing Automatic Transfer Effect

Small features of the APP, very simple, can be realized with just a few lines of code

Main page code

package com.buildingbuilding;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;
import com.buildingbuilding.activitys.BuildingActivity;
public class MainActivity extends AppCompatActivity {
  private TextView textView;
  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      if (msg.what != 0) {
        textView.setText(msg.what + "Enter APP after seconds");
      } else {
        Intent intent = new Intent(MainActivity.this, BuildingActivity.class);
        startActivity(intent);
        finish();
      }
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
  }
  private void init() {
    //Full Screen Display
    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    textView = (TextView) findViewById(R.id.textView);
    textView.setText("Pudding Pudding");
    new CountDown().start();
  }
  //Count down to enter the APP
  class CountDown extends Thread {
    int count = 3;
    @Override
    public void run() {
      try {
        while (count >= 0) {
          sleep(1000);
          Message message = new Message();
          message.what = count;
          handler.sendMessage(message);
          count--;
        }
      }
        e.printStackTrace();
      }
    }
  }
}

The basic idea is to control the main thread (i.e., the UI thread) to update the UI through a timer thread

Accept messages from the timer thread through Handler

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      if (msg.what != 0) {
        textView.setText(msg.what + "Enter APP after seconds");
      } else {
        Intent intent = new Intent(MainActivity.this, BuildingActivity.class);
        startActivity(intent);
        finish();
      }
    }
  };

2.Timer thread (inner class), set every1sleep for one second, a total of3second

//Count down to enter the APP
  class CountDown extends Thread {
    int count = 3;
    @Override
    public void run() {
      try {
        while (count >= 0) {
          sleep(1000);
          Message message = new Message();
          message.what = count;
          handler.sendMessage(message);
          count--;
        }
      }
        e.printStackTrace();
      }
    }
  }

3.Do not forget to start the thread in the init() method at last

private void init() {
    //Full Screen Display
    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    textView = (TextView) findViewById(R.id.textView);
    textView.setText("Pudding Pudding");
    new CountDown().start();
  }

OK, it's basically done now, let's see the effect

 

 

 

That's all for this article. I hope it will be helpful to everyone's study and also hope everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like