0

我想要設計一個活動,其中我有一個標題爲「以後不再顯示該畫面」的按鈕,按下該按鈕即可跳過啓動畫面,無論多少次用戶打開應用程序。
我嘗試使用android共享首選項(看到其他問題的答案),但我沒有得到所需的輸出。我在下面給出了我用過的代碼。請讓我知道代碼必須以何種方式更正。如果還有其他方法,我很高興知道這一點。 在此先感謝。在用戶首選項上僅顯示一次啓動畫面

private class MyThread extends Thread 
    { 
     public boolean bRun = true; 

     @Override 
     public void run() 
     { 
      try 
      { 
       sleep(10000); 
       if (bRun) 
       { 
        startActivity(new Intent(getApplicationContext(), PnbActivity.class)); 

       } 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
public class Preference { 

     private SharedPreferences sharedPreferences; 
     private SharedPreferences.Editor editor; 

     public Preference(Context context) { 
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
     } 

     public void writePreference(String key, Object value) { 
      if(value instanceof Boolean) { 
       editor = sharedPreferences.edit(); 
       editor.putBoolean(key, (Boolean) value); 
       editor.commit(); 

      } 
     } 

     public Object readPreference(String key , Object defValue) { 

      if(defValue instanceof Boolean) 
       return sharedPreferences.getBoolean(key, (Boolean) defValue); 
      else 
       return null; 
     } 

     public Boolean getDisableSplash() { 
      return (Boolean) readPreference("disable", false); 
     } 

     public void disableSplash(Boolean value) { 

      Object valve = null; 
      writePreference("disable", valve); 
     } 

    } 


protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_note); 


Preference preference = new Preference(Note.this); 
Boolean result = preference.getDisableSplash(); 

if(!result) { 
    // dissable you splash activity here and move to next one 
} 
thread = new MyThread(); 
thread.start();}}  
public void skipAct(View v){ 
Preference preference = new Preference(Note.this); 
preference.disableSplash(true); 
    Intent i = new Intent(Note.this, PnbActivity.class); 
    startActivity(i); 

} 
+0

設置'共享preferance'變量**點擊**和** unclicked **,然後將共享首選值設置爲**點擊**按鈕單擊事件。那麼每次檢查共享首選項的值,如果它被選中,則開始活動並跳轉到「主要活動」並調用完成,否則繼續執行splashcreen – sud

+0

現在檢查我編輯的代碼 – sud

回答

0

沒有必要只是在你的閃屏活動檢查開始閃屏前創建一個線程,在閃屏活動共享PREF值原樣

public class Splash extends Activity { 

/** Duration of wait **/ 
private final int SPLASH_DISPLAY_LENGTH = 3000; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.splashscreen); 

Button button = (Button) findViewById(R.id.button1); 

     button.setOnClickListener(new OnClickListener() { 

     @Override 

     public void onClick(View view) { 

    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
    editor.putString("status", "clicked"); 
    editor.commit(); 

     } 
    }); 

    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

String name = prefs.getString("status", "NotClicked"); 


if(name.equals("clicked"){ 
      /* Create an Intent that will start the Menu-Activity. */ 
      Intent mainIntent = new Intent(Splash.this,pnbActivity.class); 
      Splash.this.startActivity(mainIntent); 
      Splash.this.finish(); 
      } 
    /* New Handler to start the Menu-Activity 
    * and close this Splash-Screen after some seconds.*/ 
    new Handler().postDelayed(new Runnable(){ 
     @Override 
     public void run() { 

      /* Create an Intent that will start the Menu-Activity. */ 
      Intent mainIntent = new Intent(Splash.this,pnbActivity.class); 
      Splash.this.startActivity(mainIntent); 
      Splash.this.finish(); 

     } 
    }, SPLASH_DISPLAY_LENGTH); 
    } 
    } 
+0

如果用戶無論該應用程序啓動多少次,都會點擊該按鈕一次。你寫的代碼會實現嗎? – Pra

+0

捍衛共享的優先是有意義的。只要你不清除它們,共享的pref中的值就會保存下來。無論您關閉或打開應用程序多少次 – sud

+0

Sud我嘗試了您所說的。但我無法解決這個錯誤。你可以把整個代碼? – Pra

0

嘗試改變你的代碼如下圖所示:

... 
if(!result) { 
thread = new MyThread(); 
thread.start();}}  
Preference preference = new Preference(Note.this); 
preference.disableSplash(true); 
Intent i = new Intent(Note.this, PnbActivity.class); 
startActivity(i); 
} 
else 
//else if(result) 
{ 

Intent i = new Intent(Note.this, PnbActivity.class); 
startActivity(i); 
} 
... 

還要檢查How do I make a splash screen load once?

注: - 清除preference在用戶會話的時間接近再次得到SplshScreen。 希望它有效。

相關問題