Once框架使用

来源:互联网 发布:大数据建设开展情况 编辑:程序博客网 时间:2024/06/05 18:01

项目地址:
https://github.com/jonfinerty/Once

需要在module中进行配置

 compile 'com.jonathanfinerty.once:once:1.2.1'

Application中初始化使用

package jonathanfinerty.onceexample;import android.app.Application;import android.os.StrictMode;import jonathanfinerty.once.Once;public class ExampleApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        if (BuildConfig.DEBUG) {            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()                    .detectAll()                    .penaltyLog()                    .penaltyDeath()                    .build());            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()                    .detectAll()                    .penaltyLog()                    .penaltyDeath()                    .build());        }        Once.initialise(this);    }}

说下安卓开发中用到的场景:
1、App进程每个应用程序进程初始化(比如删除任务列表重新启动App),可以用到这个小开源库

 private static final String SHOW_NEW_SESSION_DIALOG = "NewSessionDialog";  if (!beenDone(Once.THIS_APP_SESSION, SHOW_NEW_SESSION_DIALOG)) {                    showDialog("This dialog should only appear once per app session");                    markDone(SHOW_NEW_SESSION_DIALOG);  }

2、App安装后,就执行一次可以用如下的代码

 private static final String SHOW_FRESH_INSTALL_DIALOG = "FreshInstallDialog";  if (!beenDone(THIS_APP_INSTALL, SHOW_FRESH_INSTALL_DIALOG)) {                    showDialog("This dialog should only appear once per app installation");                    markDone(SHOW_FRESH_INSTALL_DIALOG);}

3、App每次版本更新,就执行一次可以用如下的代码

private static final String SHOW_NEW_VERSION_DIALOG = "NewVersionDialog"; if (!beenDone(Once.THIS_APP_VERSION, SHOW_NEW_VERSION_DIALOG)) {                    showDialog("This dialog should only appear once per app version");                    markDone(SHOW_NEW_VERSION_DIALOG); }

4、每一分种只能执行一次,就执行一次可以用如下的代码

    private static final String SHOW_MINUTE_DIALOG = "OncePerMinuteDialog";if (!beenDone(TimeUnit.MINUTES, 1, SHOW_MINUTE_DIALOG)) {                    showDialog("This dialog should only appear once per minute");                    markDone(SHOW_MINUTE_DIALOG);}

5、每3秒内只能执行一次,就执行一次可以用如下的代码

  private static final String SHOW_SECOND_DIALOG = "OncePerSecondDialog";    if (!beenDone(3000L, SHOW_SECOND_DIALOG)) {                    showDialog("This dialog should only appear once 3*per second");                    markDone(SHOW_SECOND_DIALOG);  }

6、点击后第三次才执行,循序如此,就执行一次可以用如下的代码

  String buttonPressedTag = "button pressed";                markDone(buttonPressedTag);                if (beenDone(buttonPressedTag, exactly(3))) {                    Log.i("beenDone","buttonPressedTag");                    showDialog("This dialog should only appear once every three presses");                    clearDone(buttonPressedTag);                }

6、清除所有标识,回归初始化状态

 Once.clearAll();


以下是开源库中的小例子的代码。。。。

MainActivity代码

package jonathanfinerty.onceexample;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import java.util.concurrent.TimeUnit;import jonathanfinerty.once.Once;import static jonathanfinerty.once.Amount.exactly;import static jonathanfinerty.once.Once.THIS_APP_INSTALL;import static jonathanfinerty.once.Once.beenDone;import static jonathanfinerty.once.Once.clearDone;import static jonathanfinerty.once.Once.markDone;public class MainActivity extends AppCompatActivity {    private static final String SHOW_NEW_SESSION_DIALOG = "NewSessionDialog";    private static final String SHOW_FRESH_INSTALL_DIALOG = "FreshInstallDialog";    private static final String SHOW_NEW_VERSION_DIALOG = "NewVersionDialog";    private static final String SHOW_MINUTE_DIALOG = "OncePerMinuteDialog";    private static final String SHOW_SECOND_DIALOG = "OncePerSecondDialog";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Once.markDone("Application Launched");        setContentView(R.layout.activity_main);        Button oncePerSessionButton = (Button) findViewById(R.id.once_per_session_button);        oncePerSessionButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!beenDone(Once.THIS_APP_SESSION, SHOW_NEW_SESSION_DIALOG)) {                    showDialog("This dialog should only appear once per app session");                    markDone(SHOW_NEW_SESSION_DIALOG);                }            }        });        Button oncePerInstallButton = (Button) findViewById(R.id.once_per_install_button);        oncePerInstallButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!beenDone(THIS_APP_INSTALL, SHOW_FRESH_INSTALL_DIALOG)) {                    showDialog("This dialog should only appear once per app installation");                    markDone(SHOW_FRESH_INSTALL_DIALOG);                }            }        });        /**         * 每次版本更新         */        Button oncePerVersionButton = (Button) findViewById(R.id.once_per_version_button);        oncePerVersionButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!beenDone(Once.THIS_APP_VERSION, SHOW_NEW_VERSION_DIALOG)) {                    showDialog("This dialog should only appear once per app version");                    markDone(SHOW_NEW_VERSION_DIALOG);                }            }        });        /**         * 每一分钟弹一个         */        Button oncePerMinuteButton = (Button) findViewById(R.id.once_per_minute_button);        oncePerMinuteButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!beenDone(TimeUnit.MINUTES, 1, SHOW_MINUTE_DIALOG)) {                    showDialog("This dialog should only appear once per minute");                    markDone(SHOW_MINUTE_DIALOG);                }            }        });        Button oncePerSecondButton = (Button) findViewById(R.id.once_per_second_button);        oncePerSecondButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!beenDone(3000L, SHOW_SECOND_DIALOG)) {                    showDialog("This dialog should only appear once 3*per second");                    markDone(SHOW_SECOND_DIALOG);                }            }        });        Button oncePerThreePressesButton = (Button) findViewById(R.id.three_presses_button);        oncePerThreePressesButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.i("beenDone","buttonPressedTag onClick");                String buttonPressedTag = "button pressed";                markDone(buttonPressedTag);                if (beenDone(buttonPressedTag, exactly(3))) {                    Log.i("beenDone","buttonPressedTag");                    showDialog("This dialog should only appear once every three presses");//                    clearDone(buttonPressedTag);                }            }        });        Button resetButton = (Button) findViewById(R.id.reset_all_button);        resetButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Once.clearAll();            }        });    }    private void showDialog(String message) {        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();        alertDialog.setMessage(message);        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",                new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int which) {                        dialog.dismiss();                    }                });        alertDialog.show();    }}
0 0
原创粉丝点击