如何为Android中的第一个运行活动配置SharedPreferences?

时间:2015-10-27 15:35:07

标签: android sharedpreferences

我有3项活动:

1- SplashScreensActivity

2- IntroActivity

3- MainActivity

我需要做的是,首先启动应用程序启动InshActivity,然后在SplashScreenActivity之后启动MainActivity。

编辑:
我编写了这段代码但没有工作,在显示启动画面后,MainActivity Started和IntroActivity永远不会启动。

请让我知道问题出在哪里。

Intro.Java:

    SharedPreferences settings=getSharedPreferences("prefs", 0);
    final boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)
    {
        SharedPreferences.Editor editor=settings.edit();
        editor.putBoolean("firstRun",true);
        editor.commit();
        Intent i=new Intent(Intro.this,Intro.class);
        startActivity(i);
        finish();
    }
    else
    {
        Intent a=new Intent(Intro.this,MainActivity.class);
        startActivity(a);
        finish();
    }


SplashScreeActivity.Java:

public class SplashScreensActivity extends Activity {

private ImageView mLogo;
private TextView welcomeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_splash_screen);

    Typeface roboto_s = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
    mLogo = (ImageView) findViewById(R.id.logo_sp);
    welcomeText = (TextView) findViewById(R.id.welcome_text);
    welcomeText.setTypeface(roboto_s);

    Animation animation2;{
        mLogo.setAlpha(1.0F);
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate_top_to_center);
        mLogo.startAnimation(anim);
    }
    Animation animation3; {
        ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(welcomeText, "alpha", 0.0F, 1.0F);
        alphaAnimation.setStartDelay(700);
        alphaAnimation.setDuration(1200);
        alphaAnimation.start();
    }


    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        finish();
        Intent next = new Intent(getBaseContext(), Intro.class);//
        startActivity(next);
        }
    }, 2000);
}

}

1 个答案:

答案 0 :(得分:1)

SplashScreenActivity的更改解决了这个问题。
所以我在 SplashScreenActivity 中编辑了处理程序代码:

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
        public void run() {
            if (settings.getBoolean("isFirstRun", true)) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("isFirstRun", false);
                editor.commit();
                Intent next = new Intent(SplashScreensActivity.this, Intro.class);
                startActivity(next);
            } else {
                Intent next2 = new Intent(SplashScreensActivity.this, MainActivity.class);
                startActivity(next2);
                finish();
            }
        }
    }, 2000);
}

并从Intro.Java中删除firstRun方法 我希望有用