我想设计一个活动,其中我有一个标题为“以后不再显示屏幕”的按钮,无论用户打开应用程序多少次,按下该按钮都会跳过初始屏幕。
我尝试使用 android shared Preferences(查看其他问题的答案),但我没有得到想要的输出。我在下面给出了我使用的代码。请让我知道必须以何种方式更正代码。如果还有其他方法,我很高兴知道这一点。提前致谢。
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);
}