管理一次性操作的一个小的Android库:Once

jopen 9年前

管理一次性操作的一个小的Android库Once。可应用于以下场景:

  • 第一次进入App的一次性介绍说明
  • 发行说明应该只弹出一次,每一个应用程序的升级。
  • 您的应用程序只能打电话回家,以更新内容每小时一次。

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.


项目主页:http://www.open-open.com/lib/view/home/1438933661458