我如何得到一个Android应用程序来与Eclipse每天运行的方法的方法? [英] How do I get an Android application's method to run a method daily with Eclipse?

查看:222
本文介绍了我如何得到一个Android应用程序来与Eclipse每天运行的方法的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的Andr​​oid应用程序运行的方法,每天一次使用共享preferences。这里是什么我迄今的code ..

  //为onEntry广告每天运行一次
    日历C = Calendar.getInstance();
    INT lastDayRan;
    共享preferences preFS = this.getShared preferences(
              com.example.myapp,Context.MODE_PRIVATE);
    如果(prefs.getInt(lastDayRan,lastDayRan)!= TODAY){//该怎么办?    }


解决方案

如果你想确保自上次至少一天已经过去了,你跑了所需的方法,这样做:

 长minStartTime = System.currentTimeMillis的() -  1000 * 60 * 60 * 24;
长lastTimeRan = prefs.getLong(lastTimeRan,-1);
如果(lastTimeRan> = minStartTime){
        //一天天过去了。做什么?
        //也节省了新的lastTimeRan在preFS
        。prefs.edit()putLong(lastTimeRan,System.currentTimeMillis的())提交()。
}

和这将是很好如果不是拧lastTimeRan,你可以使用一个const

I am trying to make my Android app run a method once daily using SharedPreferences. Here is the code of what I have thus far..

    //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?

    }

解决方案

If you want to make sure at least one day has passed since the last time you ran the desired method, do:

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

and it would be nice if instead of wring "lastTimeRan", you would use a const

这篇关于我如何得到一个Android应用程序来与Eclipse每天运行的方法的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆