如何一天只在Android上一次显示欢迎屏幕?我正在准备一个屏幕,上面写着雨季的表情符号,当我第一次打开该应用程序时,我想一天只显示一次。
public class MainActivity extends AppCompatActivity {
private EmojiRainLayout emojiRainLayout;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emojiRainLayout = (EmojiRainLayout) findViewById(R.id.activity_main);
button = findViewById(R.id.startbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
emojiRainLayout.addEmoji(R.drawable.bell);
emojiRainLayout.addEmoji(R.drawable.coconut);
emojiRainLayout.addEmoji(R.drawable.deepam);
emojiRainLayout.addEmoji(R.drawable.flower);
emojiRainLayout.addEmoji(R.drawable.folded);
emojiRainLayout.stopDropping();
emojiRainLayout.setPer(4);
emojiRainLayout.setDuration(7200);
emojiRainLayout.setDropDuration(2400);
emojiRainLayout.setDropFrequency(500);
emojiRainLayout.startDropping();
}
});
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
解决方案:
SharedPreferences sharedPrefs = getSharedPreferences("MyPref", 0);
long time = sharedPrefs.getLong("displayedTime", 0);
if(time == 0 || time < System.currentTimeMillis() - 259200000) // 259200000 (Millisecond) = 24 Hours
{
// Show welcome screen
SharedPreferences.Editor prefsEditor = sharedPrefs.edit();
prefsEditor.putLong("displayedTime", System.currentTimeMillis()).commit();
prefsEditor.apply();
}
答案 2 :(得分:0)
//在onCreate
public static final String MyPREFERENCES = "MyPrefs";
Calendar c = Calendar.getInstance(); //get current date.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String currentDate = df.format(c.getTime());
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
boolean showWelcome = sharedpreferences.getBoolean("isShowWelcomeScreen", false);
if (showWelcome) {// normal flow
String appOpenDate = sharedpreferences.getString("appOpenDate", "");
boolean flag = compareDate(currentDate, appOpenDate);
if (!flag) {
//show welcome screen.
}
} else {
//show welcome screen.
//In Welcome screen activity/ fragment.
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("appOpenDate", currentDate); //save date when app open
editor.putBoolean("isShowWelcomeScreen", true);
editor.apply();
}