[英]Run Activity only for First time after installation of app Android
I am implementing an app which have MainActivity , onlyFirstRunActivity and alwaysRunActivity . 我正在实现一个具有MainActivity,onlyFirstRunActivity和alwaysRunActivity的应用程序。 Now i want that on app installtion or on updation of app my app cycle should be like that: 现在我希望在安装应用程序或更新应用程序时,我的应用程序周期应如下所示:
MainActivity -> onlyFirstRunActivity -> alwaysRunActivity MainActivity-> onlyFirstRunActivity-> alwaysRunActivity
and after installing or on updation my app cycle should be : 在安装或更新后,我的应用周期应为:
MainActivity -> alwaysRunActivity MainActivity-> alwaysRunActivity
How can i implement this situation 我该如何实施这种情况
You can get the update time for your app and save it in a SharedPreference. 您可以获取应用程序的更新时间,并将其保存在SharedPreference中。
Also, you should just make your main activity the always run one. 另外,您应该使主要活动始终运行。
//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);
}
To get the current_update_time: 要获取current_update_time:
How to get app install time from android 如何从Android获取应用安装时间
For last update time, see: 有关上次更新时间,请参阅:
http://developer.android.com/guide/topics/data/data-storage.html#pref http://developer.android.com/guide/topics/data/data-storage.html#pref
You can try something like this: 您可以尝试如下操作:
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.
}
Like this you can make a difference between a new install and a update. 这样,您就可以在新安装和更新之间有所作为。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.