2017-05-29 157 views
-8

我希望我的應用只能打開一次,並且不會第二次打開它如何讓我的應用程序僅打開一次,下次打開應用程序時會崩潰?

+0

放映日誌貓碼 –

+0

你可以利用sharedprefrences的檢測第一次運行,然後用android.os.Process.killProcess殺死你的應用程序 – mehulmpt

+1

@IntelliJAmiya的OP ** **都想崩潰的應用程序在第二次嘗試打開它。不需要logcat,但同意,告訴我們你的嘗試。 – Denny

回答

2

我覺得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(); 
+0

SharedPreference sharedPref = getActivty()。getPreference)...得到Red Squigly行 – Jeffrey

0

我會在第一次打開應用時在手機上創建文件(可能在某處隱藏),然後執行onCreate

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