添加数据存储

像在ios中引导页就第一次出现,之后都不会出现,一般都是使用NSUserDefault来保存一个数据。在Android中使用SharedPreferences。
安卓中欢迎界面也是自己写,那么就需要在欢迎页面指定跳转到哪里。
Handler有和多线程一样的功能,执行方法:
public class WelcomeAct extends Activity{

private boolean isFirstIn = false;
private static final int TIME = 2000;
private static final int GO_HOME = 1000;
private static final int GO_GUIDE = 1001;

private Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case GO_HOME:
                goHome();

                break;
            case GO_GUIDE:
                goGuide();
                break;
        }
    }
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.welcome);
    init();
}

private void init(){
    SharedPreferences perPreferences = getSharedPreferences("jike",MODE_PRIVATE);
    isFirstIn = perPreferences.getBoolean("isFirstIn",true);
    if (!isFirstIn){
        mHandler.sendEmptyMessageDelayed(GO_HOME,TIME);
    }else{
        mHandler.sendEmptyMessageDelayed(GO_GUIDE,TIME);
        SharedPreferences.Editor editor = perPreferences.edit();
        editor.putBoolean("isFirstIn",false);
        editor.commit();
    }
}

private void goHome(){
    Intent i = new Intent(WelcomeAct.this,MainActivity.class);
    startActivity(i);
    finish();
}
private void goGuide(){
    Intent i = new Intent(WelcomeAct.this,Guide.class);
    startActivity(i);
    finish();

}

}
根据存储的isFirst来判断跳转到哪里。

你可能感兴趣的:(添加数据存储)