android更新完之后怎么打开app,在Android中启动/更新后,检查App是否第一次打开
我想在每次第一次更新或安裝應(yīng)用程序后打開我的應(yīng)用程序時(shí)運(yùn)行服務(wù).如何在啟動(dòng)后檢查App是否首次打開?
我試過這個(gè)但是onCreate方法中的代碼,但我在更新我的代碼時(shí)得到isFirst = true,但是它應(yīng)該是空白/ null以便我可以運(yùn)行我的服務(wù).
我哪里錯(cuò)了?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isFirst = sPrefs.getString(Utility.KEY_IS_FIRST, "");
if(isFirst == null || isFirst.equalsIgnoreCase("")){
//run service and set isFirst = true
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(Activity_Home.this);
SharedPreferences.Editor edit = preferences.edit();
edit.putString(Utility.KEY_IS_FIRST, "true");
edit.commit();
}
}
解決方法:
您可以將版本代碼保存到共享首選項(xiàng),并將其與當(dāng)前運(yùn)行的應(yīng)用程序的版本代碼進(jìn)行比較.如果您的版本代碼高于保存的版本代碼,則用戶已更新,運(yùn)行您的服務(wù)并更新共享首選項(xiàng).
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int currentVersionCode = BuildConfig.VERSION_CODE;
if (prefs.getInt("LASTVERSION", 0) < currentVersionCode) {
// Run service here
prefs.edit().putInt("LASTVERSION", currentVersionCode).apply();
}
標(biāo)簽:android
來源: https://codeday.me/bug/20190724/1526043.html
總結(jié)
以上是生活随笔為你收集整理的android更新完之后怎么打开app,在Android中启动/更新后,检查App是否第一次打开的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 李现减肥歌是什么意思 李现减肥歌歌词以及
- 下一篇: android sina oauth2.