管理一次性操作的一個小的Android庫:Once
管理一次性操作的一個小的Android庫Once。可應用于以下場景:
- 第一次進入App的一次性介紹說明。
- 發行說明應該只彈出一次,每一個應用程序的升級。
- 您的應用程序只能打電話回家,以更新內容每小時一次。 </ul>
Once提供了一個簡單的API來跟蹤你的App在給定的范圍內是否已經執行相關的動作
使用
First things first, you'll need to initialise Once on start up. In yourApplicationclass'sonCreate()override add the follow:
Once.initialise(this);
Now you're ready to go. Say you wanted to navigate to a 'WhatsNew' Activity every time your app is upgraded:
String showWhatsNew = "showWhatsNewTag"; if (!Once.beenDone(Once.THIS_APP_VERSION, showWhatsNew)) { startActivity(this, WhatsNewActivity.class); Once.markDone(showWhatsNew); }
Or if you want your app tour to be shown only when a user install, simply check the tag using theTHIS_APP_INSTALLscope:
if (!Once.beenDone(Once.THIS_APP_INSTALL, showAppTour)) { ... Once.markDone(showAppTour); }
Your app operations can also be rate-limited by time spans. So for example if you only want to phone back to your a maximum of server once per hour, you'd do the following:
if (!Once.beenDone(TimeUnit.HOURS, 1, phonedHome) { ... }
To de-noise your code a bit more you can also static-import theOncemethods, so usage looks a bit cleaner
import static jonathanfinerty.once.Once.THIS_APP_INSTALL; import static jonathanfinerty.once.Once.beenDone; import static jonathanfinerty.once.Once.markDone; ... ... if (!beenDone(THIS_APP_VERSION, tagName)) { ... markDone(showWhatsNew); }
安裝
Add a library dependency to your app module'sbuild.gradle:
dependencies { compile 'com.jonathanfinerty.once:once:0.3.2' }
You'll need to havejcenter()in your list of repositories
示例
Have a look at the example app inonce-example/for more simple usage.