【问题标题】:Activity that is only launched once after a new install?新安装后仅启动一次的活动?
【发布时间】:2012-01-08 18:12:31
【问题描述】:

我希望我的应用有一个活动来显示有关如何使用该应用的说明。但是,这个“说明”屏幕只能在安装后显示一次,你是怎么做到的?

【问题讨论】:

    标签: android layout android-activity installation splash-screen


    【解决方案1】:

    您可以测试是否在您的应用程序SharedPreferences 中设置了特殊标志(我们称之为firstRun)。如果不是,这是第一次运行,因此请根据说明显示您的活动/弹出窗口/任何内容,然后在首选项中设置firstRun

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        SharedPreferences settings = getSharedPreferences("prefs", 0);
        boolean firstRun = settings.getBoolean("firstRun", true);
        if ( firstRun )
        {
            // here run your first-time instructions, for example :
            startActivityForResult(
                 new Intent(context, InstructionsActivity.class),
                 INSTRUCTIONS_CODE);
    
        }
     }
    
    
    
    // when your InstructionsActivity ends, do not forget to set the firstRun boolean
     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == INSTRUCTIONS_CODE) {
             SharedPreferences settings = getSharedPreferences("prefs", 0);
             SharedPreferences.Editor editor = settings.edit();
             editor.putBoolean("firstRun", false);
             editor.commit();
         }
     }
    

    【讨论】:

    • 应该是:editor.putBoolean("firstRun", false); 而不是editor.putBoolean("firstRun", true);
    【解决方案2】:

    是的,您可以使用 SharedPreferences 解决此问题

    SharedPreferences pref;
    SharedPreferences.Editor editor;
    
    pref = getSharedPreferences("firstrun", MODE_PRIVATE);
    editor = pref.edit();
    
    editor.putString("chkRegi","true");
    editor.commit();
    

    然后检查String chkRegi ture or false

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 1970-01-01
      • 2012-05-10
      • 2015-05-05
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多