android实现App活动定时自动跳转效果
短信预约 -IT技能 免费直播动态提醒
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,现在基本都完成了,来看效果
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341