根据用户偏好仅显示一次启动画面
Displaying Splash screen only once on user preference
我想设计一个 activity,其中有一个标题为 "Do not show the screen again in future" 的按钮,无论用户打开应用程序多少次,按下该按钮都会跳过启动画面。
我尝试使用 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);
}
...
同时勾选 How do I make a splash screen load once?
注意: - 在用户会话关闭时清除 preference
以再次获取 SplshScreen。
希望有用。
无需创建线程,就在您的启动画面中启动启动画面之前 activity 检查共享首选项值作为-
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);
}
}
我想设计一个 activity,其中有一个标题为 "Do not show the screen again in future" 的按钮,无论用户打开应用程序多少次,按下该按钮都会跳过启动画面。
我尝试使用 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);
}
...
同时勾选 How do I make a splash screen load once?
注意: - 在用户会话关闭时清除 preference
以再次获取 SplshScreen。
希望有用。
无需创建线程,就在您的启动画面中启动启动画面之前 activity 检查共享首选项值作为-
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);
}
}