假设应用程序使用以下代码检查启动是否是第一次运行:
public class MyActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Doing things
prefs = getSharedPreferences("com.example.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
}
}}
假设现在这个应用程序是通过Google Play更新的,当应用程序再次运行时,该代码会认为这是他的第一次启动吗? Sahred 首选项在更新过程中被删除?
感谢您的宝贵时间,
弗拉格。
最佳答案
我会将您的应用程序的版本存储在sharedPreferences中(而不是 bool 值)。然后将上次存储的内容与当前存储的内容进行比较。
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String currentVersion = pInfo.versionName;
if (prefs.getString("lastVersion", "0").equals(currentVersion)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putString("lastVersion", currentVersion).apply();
}
关于android - 应用更新后,下次启动算第一次运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33107203/