我希望我的应用程序(android)在第一次启动时转到另一个活动,因此人们立即更改其设置。更新我的应用程序后,我想显示更改日志。我知道你可以通过共享偏好来实现这一点,我做了类似的事情:
package me.wouter.schoolwork;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
public class Updater extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
checkFirstLaunch();
}
public void checkFirstLaunch(){
boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
if (firstrun){
new AlertDialog.Builder(this).setTitle("Welcome").setMessage("Welcome on ").setNeutralButton("OK", null).show();
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
}
}
}
我不确定这是否有效,这对更新无效。任何人都可以帮助我吗?
提前致谢:)
答案 0 :(得分:3)
试试这个:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int currentVersion = 0;
try {
currentVersion = getPackageManager().getPackageInfo(getSherlockActivity().getPackageName(), PackageManager.GET_CONFIGURATIONS).versionCode;
} catch (final NameNotFoundException e) {
}
final int lastVersion = prefs.getInt("lastVersion", -1);
if (currentVersion > lastVersion) {
showChangelogDialog();
prefs.edit().putInt("lastVersion", currentVersion).commit();
}