检查“应用程序是否首次运行”的最简单方法

检查“应用程序是否首次运行”的最简单方法

我正在为 Ubuntu Phone 编写一个应用程序,我想存储一个布尔值,该布尔值表示该应用程序是首次运行还是之前运行过。就我个人而言,我并不想创建一个完整的数据库,但如果真的需要这样做,那当然可以。

是否有任何简单的帮手可以帮我完成?

答案1

一种常见的方法是创建一个点文件在用户的主目录中:如果文件不存在,则程序是第一次运行(或者用户已要求程序表现得像第一次运行一样,方法是手动删除文件)。如果其唯一目的是执行此检查,则文件可以为空,但通常点文件也用作配置文件。例如,如果配置文件~/.zshrc在启动时不存在,Zsh shell 会打印以下内容:

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses --- 

相关内容