Android注解框架

一、简介

当下Java后端的SpringBoot微效劳框架大火,缘由离不开注解的运用,其简单易配置的注解方式使得更多的社区为其编写适用于SpringBoot的框架,也就是注解逐步取代了传统的xml配置方式。那么注解在Android中也同样的得到了升华,著名的框架有ButterKnife、Dagger2、Retrofit等等。今天带来一款Android中比拟适用的注解框架AopArms,其用法简单,里面编写了Android开发中常用的一套注解,如日志、拦截(登录)、异步处置、缓存、SP、延迟操作、定时任务、重试机制、try-catch平安机制、过滤频繁点击等,后续还会有更多更强大的注解功用参加。
本篇主要内容解说在Android中的根本用法,关于AOP在Android中的理论请参考另外一篇Android开发之AOP编程。

  一、简介   当下Java后端的SpringBoot微效劳框架大火,缘由离不开注解的运用,其简单易配置的注解方式使得更多的社区为其编写适用于SpringBoot的框架,也就是注解逐步取代了传统的xml配置方式。那么注解在Android中也同样的得到了升华,著名的框架有ButterKnife、Dagger2、Retrofit等等。今天带来一款Android中比拟适用的注解框架AopArms,其用法简单,里面编写了Android开发中常用的一套注解,如日志、拦截(登录)、异步处置、缓存、SP、延迟操作、定时任务、重试机制、try-catch平安机制、过滤频繁点击等,后续还会有更多更强大的注解功用参加。   本篇主要内容解说在Android中的根本用法,关于AOP在Android中的理论请参考另外一篇Android开发之AOP编程。   二、引入方式   1、在主工程中添加依赖   //引入aspectjx插件   applyplugin:


二、引入方式
1、在主工程中添加依赖
//引入aspectjx插件
applyplugin:’android-aspectjx’
dependencies{

implementation’cn.com.superLei:aop-arms:1.0.1′
}
2、项目跟目录的gradle脚本中参加
buildscript{
repositories{
mavenCentral()
}
dependencies{
//该库基于沪江aspect插件库
classpath’com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4′
}
}
3、在Application中初始化
AopArms.init(this);
三、根本运用
1、缓存篇(可缓存恣意类型)
1、插入缓存
@Cache(key=”userList”)
privateArrayListinitData(){
ArrayListlist=newArrayList<>();
for(inti=0;i<5;i++){
Useruser=newUser();
user.setName(“艾神一不当心:”+i);
user.setPassword(“密码:”+i);
list.add(user);
}
returnlist;
}
2、获取缓存
privateArrayListgetUser(){
returnACache.get(this).getAsList(“userList”,User.class);
}
3、移除缓存
@CacheEvict(key=”userList”)
publicvoidremoveUser(){
Log.e(TAG,”removeUser:>>>>”);
}
2、SharedPreferences篇(可保管对象)
1、保管key到sp
@Prefs(key=”article”)
privateArticleinitArticle(){
Articlearticle=newArticle();
article.author=”jerry”;
article.title=”helloandroid”;
article.createDate=”2019-05-31″;
article.content=”thisisatestdemo”;
returnarticle;
}
2、从sp中移除key
@PrefsEvict(key=”article”)
publicvoidremoveArticle(){
Log.e(TAG,”removeArticle:>>>>”);
}
3、异步篇
@Async
publicvoidasyn(){
Log.e(TAG,”useAync:”+Thread.currentThread().getName());
}
4、try-catch平安机制篇
//自动帮你try-catch允许你定义回调办法
@Safe(callBack=”throwMethod”)
publicvoidsafe(){
Stringstr=null;
str.toString();
}
//自定义回调办法(留意要和callBack的值坚持分歧)
privatevoidthrowMethod(Throwablethrowable){
Log.e(TAG,”throwMethod:>>>>>”+throwable.toString());
}
5、重试机制篇
/**
*@paramcount重试次数
*@paramdelay每次重试的距离
*@paramasyn能否异步执行
*@paramretryCallback自定义重试结果回调
*@return当前办法能否执行胜利
*/
@Retry(count=3,delay=1000,asyn=true,retryCallback=”retryCallback”)
publicbooleanretry(){
Log.e(TAG,”retryDo:>>>>>>”+Thread.currentThread().getName());
returnfalse;
}
privatevoidretryCallback(booleanresult){
Log.e(TAG,”retryCallback:>>>>”+result);
}
6、定时任务篇
/**
*@paraminterval初始化延迟
*@paraminterval时间距离
*@paramtimeUnit时间单位
*@paramcount执行次数
*@paramtaskExpiredCallback定时任务到期回调
*/
@Scheduled(interval=1000L,count=10,taskExpiredCallback=”taskExpiredCallback”)
publicvoidscheduled(){
Log.e(TAG,”scheduled:>>>>”);
}
privatevoidtaskExpiredCallback(){
Log.e(TAG,”taskExpiredCallback:>>>>”);
}
7、延迟任务篇
//开启延迟任务(10s后执行该办法)
@Delay(key=”test”,delay=10000L)
publicvoiddelay(){
Log.e(TAG,”delay:>>>>>”);
}
//移除延迟任务
@DelayAway(key=”test”)
publicvoidcancelDelay(){
Log.e(TAG,”cancelDelay:>>>>”);
}
8、过滤频繁点击
//value默许500ms
@SingleClick(value=2000L)
privatevoidonclick(){
Log.e(TAG,”onclick:>>>>”);
}
9、拦截篇(如登录)
1、在需求停止拦截的办法添加注解
@Intercept(“login_intercept”)
publicvoidloginIntercept(){
Log.e(TAG,”intercept:已登陆>>>>”);
}
2、(倡议,统一处置)在Application中停止停止监听拦截回调
publicclassMyApplicationextendsApplication{
privatestaticfinalStringTAG=”MyApplication”;
privatestaticMyApplicationmApplication;
@Override
publicvoidonCreate(){
super.onCreate();
mApplication=this;
AopArms.init(this);
AopArms.setInterceptor(newInterceptor(){
@Override
publicbooleanintercept(Stringkey,StringmethodName)throwsThrowable{
Log.e(TAG,”interceptmethodName:>>>>>”+methodName);
if(“login_intercept”.equals(key)){
StringuserId=SPUtils.get(mApplication,”userId”,””);
if(TextUtils.isEmpty(userId)){
Toast.makeText(mApplication,”您还没有登录”,Toast.LENGTH_SHORT).show();
returntrue;//代表拦截
}
}
returnfalse;//放行
}
});
}
}
以上是库的一些常用的根本用法,后续会添加更多的注解来简化Android开发,欢送前来issues来发问或者提出你以为所需求的更多注解需求。

© 版权声明
好牛新坐标
版权声明:
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com

相关文章