2012-09-13 67 views
2

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

@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 
     } 
    } 
+4

您的解决方案是合理的。唯一可能需要更改的是还要存储应用程序的版本。当用户从旧版本升级时,可能需要再次显示欢迎屏幕。但是,在更新期间不会擦除首选项,因此您当前的解决方案不处理该情况。 – Timo

回答

6

尝试用应用版本代码。以下是我使用的示例代码;

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(); 
    } 
+0

就是我今天想找的。 – localhost

+0

AlertDialogBu​​ilder是一个很好的接触,但是当初始化它时,你需要指定AlertDialog.Builder类。此外,你需要设置alertDialogBu​​ilder.show(); – Futureproof

+0

@AnujAroshA如果用户安装第二个版本而不是从第一个版本更新,这是否显示一次性警报对话框? – KJEjava48

1

而不是共享的喜好,你可以使用下面的代码还我已经使用过很多次会很好地工作,仅显示一个时候,应用程序启动第一次

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; 
} 
+0

我没有看到任何迹象表明这段代码只会在应用程序第一次使用时启动。 – Alpaslan

+0

感谢您的建议@Alpaslan对此表示歉意 –