简单判断手机APP是否是初次安装,判断手机app,方法是写在APP进入页面
分享于 点击 20638 次 点评:143
简单判断手机APP是否是初次安装,判断手机app,方法是写在APP进入页面
方法是写在APP进入页面Activity中的,才能调用下面的getFilesDir()方法:protected boolean firstsInstall() {File files = getFilesDir();/**getFilesDir()方法用于获取/data/data//files目录*/File installFile = new File(files, "install");/**新建install文件*/int newVC = 0;try {newVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;/**取得APP当前版本号newVersion */}catch (Exception e) {}boolean firstInstall = installFile.exists();if (!firstInstall) {/**文件夹不存在,则表示初次安装*/installFile.mkdirs();try {new File(installFile, newVersion + "").createNewFile();//新建一个带版本号的文件}catch (Exception e) {e.printStackTrace();}return true;}else {String[] fs = installFile.list();if (fs == null || fs.length == 0) {/**上一个版本为空,表示应用可能已经被干掉过,也相当于初次安装*/try {new File(installFile, newVersion + "").createNewFile();}catch (Exception e) {e.printStackTrace();}return true;}String lastV = fs[0];if (newVC > Integer.parseInt(lastV)) {/**如果当前 版本号大于之前版本号*/try {new File(installFile, newVersion + "").createNewFile();for (String vf : fs) {File temp = new File(installFile, vf);if (temp.exists()) temp.delete();/**删除文件*/}return true;}catch (Exception e) {e.printStackTrace();}}}return false;}
用户点评