首页 > 解决方案 > 应用程序关闭时是否可以运行计时器任务?

问题描述

我刚刚完成了 Java 的学习,现在我正在开发 Android 应用程序。但我在这个领域不是那么专家。我刚刚创建了一个 Android 应用程序,其中包含一些广告,例如横幅广告、插页式广告。如果用户观看视频广告,我想给他们一个机会在 24 小时内停止这些广告。但我不熟悉计时器任务,任何建议都会非常感激。这是我想象的代码:

// load from shared preference

SharedPreference spref = getSharedPreference("directory_name", 0);
if(spref.getBoolean("ad_key", false)==true){
    // all ads should stop showing
}else{
    // ads are showing
}


// help me here in timer task

As ad reward
--> start countadown
--> if(countDown>0){
    doneWatchindAd(true);
}else{
    doneWatchindAd(false);
}

// store to shared preference

public void doneWatchindAd(bollean cond){
    SharedPreference spref = getSharedPreference("directory_name", 0);
    SharedPreference.Editor editor = spref.edit();
    editor.putBoolean("ad_key", cond);
    editor.commit();
}

标签: runtimesharedpreferencesadstimertask

解决方案


推荐阅读