这会先看效果图:
(二)设置欢迎页并判断是否程序实第一次开启,来判断是否加载引导页。
实现关键功能:
- 1.欢迎页面的定时设置
- 2.判断 程序是否是第一次启动,如果是则跳入引导页,否则跳入主界面
- 3.记得在Mainfest文件里设置为欢迎页为启动界面
public class activity_Welcome extends AppCompatActivity implements Runnable {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcom);
//启动一个延迟线程
new Thread(this).start();
}
2.如上代码所示我们在开始,我们现在要实现run方法,延迟时间通过Thread.
sleep(
1000);这句代码来实现,括号中1000就代表延迟1秒;通过SharedPerferences 来获取程序的启动次数。在这里,如果打开程序实检测到数据count = 0,则就会判定为程序实新程序,此时我们程序实第一次打开,然后就会执行判断方法
public void run(){ try{ //延迟1秒时间 Thread.sleep(1000); SharedPreferences preferences= getSharedPreferences("count", 0); // 存在则打开它,否则创建新的Preferences int count = preferences.getInt("count", 0); // 取出数据 /** *如果用户不是第一次使用则直接调转到显示界面,否则调转到引导界面 */ if (count == 0) { Intent intent1 = new Intent(); intent1.setClass(activity_Welcome.this, activity_Guide.class); startActivity(intent1); } else { Intent intent2 = new Intent(); intent2.setClass(activity_Welcome.this, MainActivity.class); startActivity(intent2); } finish(); //实例化Editor对象 SharedPreferences.Editor editor = preferences.edit(); //存入数据 editor.putInt("count", 1); // 存入数据 //提交修改 editor.commit(); } catch (InterruptedException e) { } }
Demo地址:点击打开链接
转载请加上链接 点击打开链接
希望大家多多交流,共同学习共同进步;