如何使用Android首选项创建一次性欢迎屏幕?

如何使用Android首选项创建一次性欢迎屏幕?,android,version,sharedpreferences,Android,Version,Sharedpreferences,我想创建一个只在应用程序启动后显示一次的屏幕。之后,它将只显示主屏幕。我实现这一点的方法只是检查首选项并根据标志设置当前布局。以这种方式实施它有什么倒退吗?有更好的办法吗 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Here is the main layout setContent

我想创建一个只在应用程序启动后显示一次的屏幕。之后,它将只显示主屏幕。我实现这一点的方法只是检查首选项并根据标志设置当前布局。以这种方式实施它有什么倒退吗?有更好的办法吗

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Here is the main layout
        setContentView(R.layout.main);      

        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        // second argument is the default to use if the preference can't be found
        Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

        if (!welcomeScreenShown) {
            //Here I set the one-time layout
            setContentView(R.layout.popup_message);             
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putBoolean(welcomeScreenShownPref, true);
            editor.commit(); // Very important to save the preference
        }
    }

尝试使用应用程序版本代码。下面是我使用的示例代码

    SharedPreferences sharedPreferences = getSharedPreferences("version", 0);
    int savedVersionCode = sharedPreferences.getInt("VersionCode", 0);

    int appVershionCode = 0;

    try {
        appVershionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

    } catch (NameNotFoundException nnfe) {
        Log.w(TAG, "$ Exception caz of appVershionCode : " + nnfe);
    }   

    if(savedVersionCode == appVershionCode){
        Log.d(TAG, "$$ savedVersionCode == appVershionCode");
    }else{
        Log.d(TAG, "$$ savedVersionCode != appVershionCode");

        SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
        sharedPreferencesEditor.putInt("VersionCode", appVershionCode);
        sharedPreferencesEditor.commit();

        Builder alertDialogBuilder = new Builder(this);
        alertDialogBuilder.setTitle("Version");
        alertDialogBuilder.setMessage("This is one time show dialog box ");

        alertDialogBuilder.setNeutralButton("Close", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "$$ onClick");

            }
        });

        alertDialogBuilder.show();
    }

您可以使用下面的代码,而不是共享首选项。我也使用过很多次,它可以很好地工作。当应用程序第一次启动时,它只显示一次

public class SplashActivity extends Activity {
protected boolean _isActive = true;
protected int _splashTime = 3000; //SplashActivity will be visible for 2s
final String TAG = "SplashActivity";


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_activity);



    //a separate thread to manage splash screen
    final Thread splashThread = new Thread() {

        public void run() {

            try {
                int wait = 0;

                while (_isActive && (_splashTime > wait)) { //will show only on the first time
                    sleep(100);

                    if (_isActive) {
                        wait += 100;
                    }
                }
            } catch (InterruptedException e) {
                Log.d(TAG, e.getMessage());

            } finally {
                startActivity(new Intent(SplashActivity.this, MainActivityAbs.class));
                finish();
            }
        }
    };
    splashThread.start();
}

//if a user clicks on a back btnOrder, do not show splash screen

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _isActive = false;
    }
    return true;
}

你的解决方案是合理的。您可能想更改的唯一一件事是同时存储应用程序的版本。当用户从旧版本升级时,您可能希望再次显示欢迎屏幕。但是,在更新过程中不会删除首选项,因此您当前的解决方案无法处理这种情况。这正是我今天所要查找的。AlertDialogBuilder很不错,但是您需要在初始化它时指定AlertDialog.Builder类。此外,还需要设置alertDialogBuilder.show()@AnujAroshA:如果用户安装了第二个版本而不是从第一个版本更新,是否会显示一次性警报对话框?@AnujAroshA我认为当现有用户更新应用程序时,他会根据您的代码再次看到一次性警报。我看不到任何迹象表明此代码只会在第一次更新时启动应用程序的用法。感谢您的建议@Alpaslan抱歉