在用户首选项上仅显示一次启动画面

问题描述:

我想要设计一个活动,其中我有一个标题为“以后不再显示该画面”的按钮,按下该按钮即可跳过启动画面,无论多少次用户打开应用程序。
我尝试使用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

没有必要只是在你的闪屏活动检查开始闪屏前创建一个线程,在闪屏活动共享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

尝试改变你的代码如下图所示:

... 
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。 希望它有效。