如何获得Android应用程序的方法来每天使用Eclipse运行方法?

cle*_*vor 0 eclipse android sharedpreferences

我试图让我的Android应用程序每天使用SharedPreferences运行一次方法.这是我到目前为止的代码..

    //for onEntry ad to run once daily
    Calendar c = Calendar.getInstance(); 
    int lastDayRan;
    SharedPreferences prefs = this.getSharedPreferences(
              "com.example.myapp", Context.MODE_PRIVATE);
    if(prefs.getInt("lastDayRan", lastDayRan) != today){//what to do now?

    }
Run Code Online (Sandbox Code Playgroud)

dor*_*ors 5

如果您想确保自上次运行所需方法后至少已过去一天,请执行以下操作:

long minStartTime = System.currentTimeMillis() - 1000*60*60*24;
long lastTimeRan = prefs.getLong("lastTimeRan", -1);
if (lastTimeRan >= minStartTime) {
        // a day passed. do whatever...
        //also save the new 'lastTimeRan' in prefs
        prefs.edit().putLong("lastTimeRan", System.currentTimeMillis()).commit();
}
Run Code Online (Sandbox Code Playgroud)

如果不是拧"lastTimeRan",你会使用const,这将是很好的