2016-03-04 73 views
0

我只需要运行一次函数(当它是夜晚,更改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(); 
    } 
} 
} 
+0

每次,它显示动画,我不知道该怎么做,如果我只是想让它显示,当我第一次在夜晚开始它的时候 – user5607014

回答

1

文件或存储在共享首选项中。例如,用于保存方法:

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

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