一个新的app一般会有引导页,启动页。启动页每次打开app都会有,但是引导页只有第一次进入app才会打开,这就需要我们进行一波判断。
SharedPreferences sharedPreferences = getSharedPreferences("share", MODE_PRIVATE);
//默认false
boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", false);
SharedPreferences.Editor editor = sharedPreferences.edit();
//如果是true就表示已经不是第一次运行app,跳转相应界面就可以了 反之。。。。。。
if (isFirstRun) {
//不是第一次运行
Intent intent = new Intent();
intent.setClass(GuideActivity.this, MainActivity.class);
startActivity(intent);
} else {
//第一次运行
editor.putBoolean("isFirstRun", true);
editor.commit();
Intent intent = new Intent();
intent.setClass(GuideActivity.this, StartActivity.class);
startActivity(intent);
}
另外还有就是启动页延时几秒进入主界面的问题一块说了,他们都是连着的,我这里列举四种方法:
1:线程休眠
new Thread() {
@Override
public void run() {
super.run();
try {
Thread.sleep(3000);//休眠3秒
//这里写具体的跳转操作
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
2:TimerTask
TimerTask task = new TimerTask() {
@Override
public void run() {
//这里写具体的跳转操作
}
};
Timer timer = new Timer();
timer.schedule(task, 3000);//3秒后执行TimeTask的run方法
3:Handler的postDelayed方法
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//这里写具体的跳转操作
}
}, 3000);//3秒后执行Runnable中的run方法
4:rxjava实现首先添加rxJava依赖,我以下面版本为例
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
然后在执行延时跳转操作的地方
Observable.timer(3, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aLong -> {
startActivity(new Intent(this,MainActivity.class));finish();
});
关于找一找教程网
本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[Android判断APP是否第一次进入]http://www.zyiz.net/tech/detail-93425.html