2

所以...我开发了一个成功运行的启动画面。我怎样才能让它运行一次(并且只运行一次)?我想建立一个注册屏幕,但我只希望它为用户显示一次。

帮助!

阿曼尼·斯旺

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import com.nfc.linkingmanager.R;

public class SplashScreen extends Activity {

private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 1000; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash_screen);

    Handler handler = new Handler();


    handler.postDelayed(new Runnable() {

        @Override
        public void run() {



            finish();

            if (!mIsBackButtonPressed) {

                Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
           }

        }

    }, SPLASH_DURATION);

}

 @Override
 public void onBackPressed() {
    mIsBackButtonPressed = true;
    super.onBackPressed();

}
}
4

5 回答 5

3

用于SharedPreferences存储屏幕已经显示的事实;开始时,检查此项,如果是,则通过启动下一个并finish()立即调用启动屏幕来替换 Activity。

于 2013-03-18T14:43:15.970 回答
1

您需要像这样在共享首选项中存储一个标志

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
finish();
if (prefs.getBoolean("SplashFlag", false)&&!mIsBackButtonPressed){

 Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
}else {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("SplashFlag", true); // value to store
//again there are values for int, long, string, float, boolean
editor.commit();//This is needed or the edits will not be put into the prefs file
}
于 2013-03-18T15:45:11.407 回答
0

使用SharedPreferences你可以存储一个你可以默认boolean为假的实例。seenSplash然后使用 if 检查它是否为假,然后显示启动画面。在用户看到启动画面后Editor,您可以将布尔值更改为 true。

不要忘记commit()编辑器上的更改。

于 2013-03-18T14:45:28.193 回答
0

在您的注册屏幕中获取登录详细信息并与您在共享首选项中保存的值进行比较。或硬编码,例如用户名是管理员,密码是管理员。这两个值都是正确的,只是为了进入启动画面,否则无论你喜欢什么都重定向到其他页面。

于 2013-03-18T14:54:31.387 回答
0

有两种方法可以做到这一点

第一

finish();
SplashScreen.this.startActivity(intent);

在您的 Android 清单中,您在其中声明了启动屏幕活动的第 2 位

添加这个

android:noHistory=true
于 2013-03-18T14:55:59.363 回答