【问题标题】:Launch an activity only once after Install安装后仅启动一次活动
【发布时间】:2017-05-01 05:46:01
【问题描述】:

我有 QR 扫描仪应用程序。应用程序中有 3 个活动。

1) 主要活动 - 打开相机并开始扫描的按钮

2) 二维码活动 - 扫描二维码

3) Web Activity - 扫描成功后,在应用中打开一个网页

此处,Main Activity 和 QR Activity 应仅在初始安装后启动一次。我在某处读到了有关使用共享首选项的信息。但是我有点困惑我在哪里检查变量,比如在哪个活动中。我应该在主活动中检查我的共享变量吗?

这是我的第一个应用程序。抱歉,如果这是一个愚蠢的疑问。

【问题讨论】:

    标签: android android-activity sharedpreferences splash-screen


    【解决方案1】:
    My MainActivity.java
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    
        new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                SharedPreferences settings = getSharedPreferences("prefName",MODE_PRIVATE);
                boolean firstLaunch = settings.getBoolean("firstLaunch", true);
                if(firstLaunch == false) {
                    SharedPreferences.Editor editor=settings.edit();
                    editor.putBoolean("firstRun",true);
                    editor.commit();
                    Intent i = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
                else {
                    Intent i = new Intent(SplashActivity.this, ScannerActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        }, SPLASH_TIME_OUT);
    }
    

    在新的 Splash Activity 中编写此代码

    【讨论】:

      【解决方案2】:

      正如码头所说,保存它曾经见过一次是要走的路。但是,我发现在一些旧设备上,共享偏好并不可靠!

      我建议改用 SQLite 数据库。

      如下创建表

      TABLE NAME: SEEN_ACTIVITY
      Column 1: ID INT PRIMARY KEY
      Column 2: SEEN VARCHAR
      

      然后,一旦 Activity 启动,检查 SEEN_ACTIVITY 中是否有 id = '0' 的记录。如果不是,则插入一个如下,(0, true)。

      然后,每次应用启动时,检查是否存在 (0, true) 的记录。如果没有,请启动额外的活动。

      【讨论】:

        【解决方案3】:

        没错,你必须使用SharedPreferences

        Here is a good explaination about how to use them

        在显示的第一个活动中,您必须在 onCreate 方法中添加这些行:

        //this retrieve the sharedpreference element
        SharedPreference myPref = this.getSharedPreferences(
          "prefName", Context.MODE_PRIVATE);
        //this retrieve the boolean "firstRun", if it doesn't exists, it places "true"
        var firstLaunch = myPref.getBoolean("firstLaunch", true);
        //so, if it's not the first run do stuffs
        if(!firstLaunch){
          //start the next activity
          finish();
        }      
        //else, if it's the first run, add the sharedPref
        myPref.edit().putBoolean("firstLaunch", false).commit();
        

        希望对你有帮助

        【讨论】:

        • 谢谢。但我有一个疑问。我应该在哪里写上面的代码?在我的主要活动中?
        • 我在 onCreate 方法启动的第一个活动上写的 :)
        【解决方案4】:

        要完成@Pier Giorgio Misley 的回答,您可以将“firstLaunch”检查放在您的主要活动中,或者将其放在另一个“飞溅”活动中

        要将其放入主 Activity,只需将 ui 设置为某种中性色,直到您决定是否应该完成 Activity 并启动 Web Activity 或显示主 Activity 逻辑

        或者,您可以创建一个“启动”屏幕,它可以用作桥接活动(显示一些徽标或漂亮的背景颜色),它检查变量并决定打开哪个活动Android splash

        【讨论】:

        • 嘿,我用共享首选项编辑了我的答案。但我的应用崩溃了
        • @SMG 您的 sharedPrefrences 检查看起来不错 购买 为什么要在处理程序中执行检查?参加活动 onCreate,关于错误显示 logcat,以便我们提供帮助
        • 您好,感谢您的帮助。我用工作应用编辑了我的答案..
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        相关资源
        最近更新 更多