仅在新安装后启动一次的活动?

bor*_*mke 6 layout android install splash-screen android-activity

我希望我的应用程序有一个活动,显示如何使用该应用程序的说明.但是,这个"指令"屏幕只能在安装后显示一次,你是怎么做到的?

Emm*_*Sys 16

您可以firstRun在应用程序中设置一个特殊标志(让我们称之为)SharedPreferences.如果没有,则是第一次运行,因此请按照说明显示您的活动/弹出/其他内容,然后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();
     }
 }
Run Code Online (Sandbox Code Playgroud)

  • 它应该是:`editor.putBoolean("firstRun",false);`而不是`editor.putBoolean("firstRun",true);` (2认同)