仅在Android首次启动时显示设置屏幕

时间:2012-01-02 16:24:31

标签: android screen installation

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

我怎样才能实现这一目标? 请提前帮助和谢谢!

2 个答案:

答案 0 :(得分:13)

使用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

答案 1 :(得分:0)

进行一项帮助活动。这将是你的启动器活动。它不会包含任何布局,它只会检查应用程序的第一次新运行。如果它首先运行,则将启动安装活动,否则将启动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();
        }
    }
}
相关问题