2013-02-02 32 views
0

我有一個應用程序顯示啓動畫面,然後顯示'Showonce'畫面,我已經使用應用程序首選項和布爾型「user_accept」來決定是否顯示或不顯示畫面,如果boolean = true它仍然顯示頁面,請參閱我的代碼頁面,任何幫助非常感謝:)。「顯示一次」屏幕布爾問題

package com.overclockerz.webtest; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.os.Handler; 
import android.preference.PreferenceManager; 
import android.view.Window; 
import android.widget.Toast; 



public class SplashScreen extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.splash_layout); 
     final SharedPreferences settings = getSharedPreferences("APP_PREFS", 
       MODE_PRIVATE); 
     final boolean hasAgreed = settings.getBoolean("user_accepted", false); 
     Handler handler = new Handler(); 

     // run a thread after 2 seconds to start the home screen 
     handler.postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       if (hasAgreed == false){ 
        Intent intent = new Intent(SplashScreen.this, ShowOnce.class); 
        SplashScreen.this.startActivity(intent); 
        Toast.makeText(getApplicationContext(), "DEBUG: USER HAS NOT ACCEPTED",Toast.LENGTH_LONG).show(); 
       }else if (hasAgreed == true){ 
        Toast.makeText(getApplicationContext(), "DEBUG: USER HAS ACCEPTED",Toast.LENGTH_LONG).show(); 
        Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
        startActivity(intent); 
        finish(); 
       } 

       finish(); 
      } 

     }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called 

    } 
} 



package com.overclockerz.webtest; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.Toast; 

public class ShowOnce extends Activity implements OnClickListener { 
    Button show_options_dialog, accept_button; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.show_once); 
     show_options_dialog = (Button) findViewById(R.id.show_options_dialog); 
     accept_button = (Button) findViewById(R.id.accept_button); 
     show_options_dialog.setOnClickListener(this); 
     accept_button.setOnClickListener(this); 


     } 

    @Override 
    public void onClick(View v) { 
     if (v == show_options_dialog){ 
      Intent intent = new Intent(getApplicationContext(), SettingScreen.class); 
      startActivity(intent); 
     } 
     else if (v == accept_button){ 
      SharedPreferences settings = getSharedPreferences("APP_PREFS", 
        MODE_PRIVATE); 
      SharedPreferences.Editor prefEditor = settings.edit(); 
      prefEditor.putBoolean("user_accept", true); 
      Toast.makeText(getApplicationContext(), "DEBUG: USER CLICKED ACCEPT", Toast.LENGTH_LONG).show(); 
      prefEditor.commit(); 
      Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
      startActivity(intent); 
      finish(); 
     } 

    } 

} 

回答

2

要保存user_accept和檢查user_accepted

請使用公共靜態最後絃樂避免這樣的問題:

public static final String USER_ACCEPTED = "accepted"; 
......... 
prefEditor.putBoolean(USER_ACCEPTED , true); 
......... 
settings.getBoolean(USER_ACCEPTED , false); 
+0

謝謝你,我知道這將是這麼簡單的東西,Couldn」不要再爲樹林看樹了,再次感謝你!一整天都讓我發狂。 – Pheonix2105

+0

正等着計時器:) – Pheonix2105