像這樣你可以區分新的安裝和更新。
String StoredVersionname = "";
String VersionName;
AlertDialog LoginDialog;
AlertDialog UpdateDialog;
AlertDialog FirstRunDialog;
SharedPreferences prefs;
public static String getVersionName(Context context, Class cls) {
try {
ComponentName comp = new ComponentName(context, cls);
PackageInfo pinfo = context.getPackageManager().getPackageInfo(
comp.getPackageName(), 0);
return "Version: " + pinfo.versionName;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
return null;
}
}
public void CheckFirstRun() {
VersionName = MyActivity.getVersionName(this, MyActivity.class);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
StoredVersionname = (prefs.getString("versionname", null));
if (StoredVersionname == null || StoredVersionname.length() == 0){
FirstRunDialog = new FirstRunDialog(this);
FirstRunDialog.show();
}else if (StoredVersionname != VersionName) {
UpdateDialog = new UpdateDialog(this);
UpdateDialog.show();
}
prefs.edit().putString("versionname", VersionName).commit();
}
SharedPreferences可以存儲一個布爾型'bFirstRun',告訴你這是否是第一次運行。 – 2013-02-08 16:50:39
但最後存儲的sharedprefences仍然在updation.Means如何配置應用程序更新更新? – User42590 2013-02-08 16:57:15
不確定你的意思,但是如果沒有名爲'bFirstRun'的SharedPreference ...那麼你是第一次運行。然後將其設置爲false(並提交更改)。下一次通過,它在那裏和假所以轉到'alwaysRunActivity' – 2013-02-08 17:00:18