如何每天仅在Android上一次显示欢迎屏幕?

时间:2018-10-31 06:33:57

标签: android

如何一天只在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();
            }
        });


    }
}

3 个答案:

答案 0 :(得分:1)

  1. 第一次显示时,将currentTime保存到SharedPreferences中,例如将System.currentTimeMillis()保存为t1,
  2. 第二次,通过System.currentTimeMillis()获取当前时间t2,
  3. 读取您的t1,并通过t1和t2中的新SimpleDateFormat(“ yyyy-MM-dd”)获得day1和day2,
  4. 比较day1和day2,如果day2大于day1,则显示它并在SharedPreference中更新您的t1,或者day1和day2相同,则什么也不做。

答案 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();
    }