我正在实现一个具有MainActivity,onlyFirstRunActivity和alwaysRunActivity的应用程序。现在我希望应用程序安装或更新的
MainActivity - > onlyFirstRunActivity - > alwaysRunActivity
安装或更新后和 我的应用周期应为:
MainActivity - > alwaysRunActivity
我如何实现这种情况
答案 0 :(得分:1)
您可以获取应用的更新时间并将其保存在SharedPreference中。
此外,您应该让您的主要活动始终运行。
//In your onCreate for your main activity/.
if(last_update_preference < current_update_time){
Update last update preference to current update time.
Run first activity. (Which will finish and bounce you back);
}
获取current_update_time:
How to get app install time from android
有关上次更新时间,请参阅:
http://developer.android.com/guide/topics/data/data-storage.html#pref
答案 1 :(得分:0)
您可以尝试这样的事情:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App app = (App)getApplication();
if (app.getApi() != null){
super.onBackPressed();
startActivity(new Intent(this, MainActivity.class));
return;
}else showLoginForm();//or do something else.
}
答案 2 :(得分:0)
像这样,您可以在新安装和更新之间做出改变。
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();
}