【问题标题】:Showing the setup screen only on first launch in android仅在 android 中首次启动时显示设置屏幕
【发布时间】:2012-01-02 16:24:30
【问题描述】:

我正在制作一个 android 应用程序,但我不知道如何才能使设置屏幕仅在第一次出现。 这是应用程序的工作方式: 用户在安装后启动应用程序并显示欢迎/设置屏幕。并且一旦用户完成设置,除非用户重新安装应用程序,否则设置屏幕将永远不会再次出现。

我怎样才能做到这一点??? 请帮助并提前非常感谢!

【问题讨论】:

    标签: android screen installation


    【解决方案1】:

    使用SharedPreferences测试是否是第一次启动。

    注意:以下代码未经测试。

    在你的 onCreate 中(或任何你想做的事情,取决于第一次开始与否),添加

    // here goes standard code 
    
    SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
    
    if(pref.getBoolean("firststart", true)){
       // update sharedpreference - another start wont be the first
       SharedPreferences.Editor editor = pref.edit();
       editor.putBoolean("firststart", false);
       editor.commit(); // apply changes
    
       // first start, show your dialog | first-run code goes here
    }
    
    // here goes standard code
    

    【讨论】:

    • 那么默认代码在哪里,第一次运行的代码在哪里?
    • @MySoftware 我已将提示添加为 cmets
    • 哦,抱歉,没有看到“//这里是标准代码”哈哈。但我在“getSharedPreferences”上收到此错误:ContextWrapper 类型中的方法 getSharedPreferences(String, int) 不适用于参数 (String)
    • 我刚刚看了你的其他问答。不要个人认为,但如果你不能解决非常直接的警告或提出良好的英语风格的问题,那么你可能还没有准备好编程。
    • 如果是,请将答案标记为最终答案:)。你可以通过点击这篇文章左边的绿色勾号来做到这一点。
    【解决方案2】:

    进行一项辅助活动。这将是您的启动器活动。它不会包含任何布局,它只会检查应用程序的首次全新运行。如果它将首先运行,则将启动设置活动,否则将启动 MainActivity。

    public class HelperActivity extends Activity {
    
        SharedPreferences prefs = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Perhaps set content view here
    
            prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            if (prefs.getBoolean("firstrun", true)) {
                // Do first run stuff here then set 'firstrun' as false
                //strat  DataActivity beacuase its your app first run
                // using the following line to edit/commit prefs
                prefs.edit().putBoolean("firstrun", false).commit();
                startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
                finish();
            }
            else {
            startActivity(new Intent(HelperActivity.ths , MainActivity.class));
            finish();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      相关资源
      最近更新 更多