简体   繁体   English

安装Android应用后仅首次运行活动

[英]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.

相关问题 仅在首次安装应用程序后创建的文件 - ANDROID - File created only after first installation of the app - ANDROID 仅在首次运行时执行Android运行设置活动 - Android run setup activity on first run only 安装后,Android App第一次加载时间太长,显示空白屏幕 - Android App is taking too much time to load for the first time after installation, showing blank screen 如何更改仅在应用启动时首次启动的 Android 应用活动 - How to change the Android app activity which starts only for first time on app startup 我的Android应用程序无法在安装后首次运行时调用https - My Android app does not work on https call at First Run After Installation 我是否可以在第一次打开应用程序时运行Android活动? - Can I have an android activity run only on the first time an application is opened? 如何在更新后首次打开应用程序时仅启动一次活动? - How to launch activity only once when app is opened for first time after an update? 在手机上首次运行该应用程序时,活动只能运行一次 - Activity should run only once at the first run of the App in phone 安装应用程序后在 android 工作室中启动活动时出错 - Error While launching activity in android studio after the app installation 用户权限检查仅在安装后第一次生效 - User permission check only works first time after installation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM