Java 根据用户偏好仅显示一次启动屏幕

Java 根据用户偏好仅显示一次启动屏幕,java,android,multithreading,splash-screen,dynamic-splash-screen,Java,Android,Multithreading,Splash Screen,Dynamic Splash Screen,我想设计一个活动,其中我有一个标题为“以后不再显示屏幕”的按钮,无论用户打开应用程序多少次,按下该按钮都会跳过启动屏幕。 我尝试使用android共享偏好(其他问题见答案),但我没有得到想要的输出。我在下面给出了我使用的代码。请让我知道必须以何种方式更正代码。如果还有其他方法,我很高兴知道这一点。 提前谢谢 private class MyThread extends Thread { public boolean bRun = true; @Over

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

}

尝试更改代码,如下所示:

...
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);
}
...
也检查

注意:-在用户会话关闭时清除
首选项
,以再次获取SplshScreen。
希望它能工作。

无需创建线程,就在启动启动屏幕之前,在启动屏幕活动中检查共享的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);
   }
  }  

在启动屏幕活动中,将
共享首选项
变量设置为单击和取消单击,然后将共享首选项值设置为单击
按钮单击事件
。然后每次检查共享首选项的值(如果选中),启动活动并跳转到
主活动
,然后调用finish,否则继续执行SplashCreen Now check my edited code,因为无论应用程序启动多少次,如果用户单击按钮一次,启动屏幕都不能执行。你所写的代码能做到这一点吗?防卫性的共享偏好意味着这一点。共享优先级中的值是保留的,只要您不清除它们。无论你关闭或打开应用程序多少次,我都会尝试你所说的。但我无法解决这个错误。你能把整个代码放进去吗?现在显示一个错误,我的名字不能被解析。如何修复此问题?
公共静态最终字符串MY\u PREFS\u NAME=“MyPrefsFile”添加此