zoukankan      html  css  js  c++  java
  • 检測用户是否是第一次打开应用

    非常多时候我们须要检測用户是不是第一次打开应用,从而初始化一些数据,或者打开引导界面等等。


    /*方法一:
     * 首次打开的时候获取isFirstIn值,默认值为false
     * 获得false,证明不是第一次打开
     * 获得true,证明是第一次打开;然后把isFirstIn值设为false
     * */
    SharedPreferences sp = getSharedPreferences("isFirstIn", Activity.MODE_PRIVATE);  
    boolean isFirstIn = sp.getBoolean("isFirstInWith1.4", true);  
    if(isFirstIn) {  
        SharedPreferences.Editor editor = sp.edit();  
        editor.putBoolean("isFirstInWith1.4", false);  
        editor.commit();  
          
        new AlertDialog.Builder(this).setMessage("这是第一次打开").show();  
    } else {  
        new AlertDialog.Builder(this).setMessage("你打开了n次了").show();  
    }

    注:由于在用户安装新版本号的时候,我们想要的也是显示第一次打开,可是版本号更新是会保留上一版本号的数据的,所以检測到的是之前有打开过。

    所以我们在每个版本号检測的key中增加版本号号,如上面的isFirstInWith1.4,当中1.4就是版本号号。

    /*方法二:
     * 在首次打开的时候检查是否存在文件(com.example.test.isFirstIn)
     * 假设已存在,证明不是第一次打开
     * 不存在,证明是第一次打开;打开之后创建文件
     * */
    File dir = getFilesDir();//   /data/data/com.example.test/files
    File f = new File(dir, "com.example.test.isFirstIn");
    Log.e("miquan", f.getAbsolutePath());
    
    if(f.exists()) {
    	//dosomething
    	new AlertDialog.Builder(this).setMessage("你打开了n次了").show();
    } else {
    	try {
    		f.createNewFile();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	//dosomething
    	new AlertDialog.Builder(this).setMessage("这是第一次打开").show();
    }


  • 相关阅读:
    Java Output流写入包装问题
    SpringBoot项目单元测试不经过过滤器问题
    SpringSecurity集成启动报 In the composition of all global method configuration, no annotation support was actually activated 异常
    JWT jti和kid属性的说明
    Maven 排除依赖
    第五章 基因概念的发现
    第三章 孟德尔遗传的拓展
    第二章 孟德尔遗传
    第一章 引言
    GWAS全基因组关联分析
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4232148.html
Copyright © 2011-2022 走看看