android实现App活动定时自动跳转效果
作者:小黄鸭会发光丶
本篇文章主要介绍了android实现App活动定时自动跳转效果,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
App的小功能点,很简单几十行代码就可以实现
主页面代码
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 + "秒后进入APP"); } 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() { //全屏显示 getSupportActionBar().hide(); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); textView = (TextView) findViewById(R.id.textView); textView.setText("布丁布丁"); new CountDown().start(); } //进入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--; } } catch (InterruptedException e) { e.printStackTrace(); } } } }
基本思路就是,通过一个计时线程来控制主线程(即UI线程)来更新UI
通过Handler来接受来自计时线程的Message
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what != 0) { textView.setText(msg.what + "秒后进入APP"); } else { Intent intent = new Intent(MainActivity.this, BuildingActivity.class); startActivity(intent); finish(); } } };
2.计时线程(内部类),设置每隔1秒睡一次,共3秒
//进入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--; } } catch (InterruptedException e) { e.printStackTrace(); } } }
3.最后别忘了在init()方法中启动线程
private void init() { //全屏显示 getSupportActionBar().hide(); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); textView = (TextView) findViewById(R.id.textView); textView.setText("布丁布丁"); new CountDown().start(); }
OK,现在基本都完成了,来看效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。