欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-20 11:04:04
app的小功能点,很简单几十行代码就可以实现 主页面代码 package com.buildingbuilding; import android.c...

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

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

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

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

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

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

上一篇:

下一篇: