2016-03-04 71 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()時,請閱讀此文件。