如何让我的应用程序只打开一次,下次打开应用程序时它会崩溃?

时间:2017-05-29 07:46:33

标签: java android

我希望我的应用程序只打开一次,并且第二次打开它时不会运行

2 个答案:

答案 0 :(得分:1)

我认为SharedPreferences是更好的主意,数据比文件更安全。

只需将其粘贴到onCreate()即可。

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Boolean expired= sharedPref.getBoolean("expired", false);

if (expired){
    throw new RuntimeException("Custom crash");
}

//Make it expierd
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("expired", true);
editor.commit();

答案 1 :(得分:0)

首次打开应用时,我会在手机上创建一个文件(可能隐藏在某个地方),然后在onCreate中执行以下操作:

File file = new File("path/to/file);
if(file.exists()) {
    throw new RuntimeException("This is a crash"); //Let app crash
}
相关问题