亲宝软件园·资讯

展开

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

小黄鸭会发光丶 人气:0
想了解android实现App活动定时自动跳转效果的相关内容吗,小黄鸭会发光丶在本文为您仔细讲解android 定时跳转的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,定时跳转,android实现定时,android实现页面跳转,下面大家一起来学习吧。

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

这里写图片描述 

这里写图片描述 

这里写图片描述 

这里写图片描述

加载全部内容

相关教程
猜你喜欢
用户评论