android实现App活动定时自动跳转效果

时间:2021-05-20

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,现在基本都完成了,来看效果




以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章