Android 如何只运行一次函数

Android 如何只运行一次函数,android,Android,我只需要运行一次函数(夜间时,更改imageview的图像),当我在oncreate()中使用它时,它会在每次启动时运行 活动 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startAnim(); } } private voi

我只需要运行一次函数(夜间时,更改imageview的图像),当我在oncreate()中使用它时,它会在每次启动时运行 活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
                startAnim();
    }
}

private void startAnim(){
    Date dateNow=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    String night=String.format("%tF",dateNow)+" 19:00:00";
    try {
        Date dateNight=sdf.parse(night);
        if(dateNow.after(dateNight)){
             DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            int width = metric.widthPixels;  // 屏幕宽度(像素)
            int height = metric.heightPixels;  // 屏幕高度(像素)
            RotateAnimation ra=new RotateAnimation(0,100,width/2,height/2-80);
            ra.setDuration(4000);
            sunMoon.startAnimation(ra);
              } catch (ParseException e) {
        e.printStackTrace();
    }
}
}

在文件中记录上次运行startAnim()的时间。启动活动时阅读此文件,以决定是否运行
startAnim()

将上次运行
startAnim()
的时间记录在文件中。启动活动时阅读此文件,以决定是否运行
startAnim()

共享首选项中的文件或存储。保存方法的示例:

private void saveLastRanTime(String key, long lastRunTime) {
    SharedPreferences prefs = getApplicationContext().getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putLong(key, lastRunTime); // Store the key somewhere instead of passing in each time
    editor.apply();
}
示例检查:

private boolean wasLastRunToday(String keyOfPreference) {
    SharedPreferences prefs = getApplicationContext().getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    long lastRanAt = prefs.getLong(keyOfPreference, -1); // Save key somewhere..
    if (lastRanAt == -1) { // In the event it was never saved before.
       return false;
    }

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(lastRanAt);
    int dayLastRanAt = cal.get(Calendar.DAY_OF_YEAR);

    cal.setTimeInMillis(System.currentTimeMillis());
    int today = cal.get(Calendar.DAY_OF_YEAR);

    return today == dayLastRanAt;
}
这会使startAnim()方法看起来更像:

private void startAnim() {
    if (wasLastRunToday("LAST_ANIMIATION_RUNTIME")) {
        return;
    }

    Date dateNow=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    String night=String.format("%tF",dateNow)+" 19:00:00";
    try {
        Date dateNight=sdf.parse(night);
        if(dateNow.after(dateNight)) {
             DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            int width = metric.widthPixels;  // 屏幕宽度(像素)
            int height = metric.heightPixels;  // 屏幕高度(像素)
            RotateAnimation ra=new RotateAnimation(0,100,width/2,height/2-80);
            ra.setDuration(4000);
            sunMoon.startAnimation(ra);
            saveLastRanTime("LAST_ANIMIATION_RUNTIME", dateNow.getTime());
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

共享首选项中的文件或存储。保存方法的示例:

private void saveLastRanTime(String key, long lastRunTime) {
    SharedPreferences prefs = getApplicationContext().getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putLong(key, lastRunTime); // Store the key somewhere instead of passing in each time
    editor.apply();
}
示例检查:

private boolean wasLastRunToday(String keyOfPreference) {
    SharedPreferences prefs = getApplicationContext().getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    long lastRanAt = prefs.getLong(keyOfPreference, -1); // Save key somewhere..
    if (lastRanAt == -1) { // In the event it was never saved before.
       return false;
    }

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(lastRanAt);
    int dayLastRanAt = cal.get(Calendar.DAY_OF_YEAR);

    cal.setTimeInMillis(System.currentTimeMillis());
    int today = cal.get(Calendar.DAY_OF_YEAR);

    return today == dayLastRanAt;
}
这会使startAnim()方法看起来更像:

private void startAnim() {
    if (wasLastRunToday("LAST_ANIMIATION_RUNTIME")) {
        return;
    }

    Date dateNow=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    String night=String.format("%tF",dateNow)+" 19:00:00";
    try {
        Date dateNight=sdf.parse(night);
        if(dateNow.after(dateNight)) {
             DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            int width = metric.widthPixels;  // 屏幕宽度(像素)
            int height = metric.heightPixels;  // 屏幕高度(像素)
            RotateAnimation ra=new RotateAnimation(0,100,width/2,height/2-80);
            ra.setDuration(4000);
            sunMoon.startAnimation(ra);
            saveLastRanTime("LAST_ANIMIATION_RUNTIME", dateNow.getTime());
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

每次我开始活动时,它都会显示动画,如果我只是想在每天晚上第一次开始活动时显示动画,我不知道该怎么办每次我开始活动时,它都会显示动画,如果我只是想在每天晚上第一次开始活动时显示动画,我不知道该怎么办