Android-AOP 动态代理demo

aop-动态代理方式,简单登录demo

失败
成功
首页
登录验证
登录
主页面

首页MainActivity

public class MainActivity extends AppCompatActivity implements ILogin {

    private ILogin proxy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        proxy = (ILogin) Proxy.newProxyInstance(this.getClassLoader(), new Class[]{ILogin.class}, new MyHandler(this, this));
    }

    public void testLogin(View view) {
        proxy.loginPass();
    }

    public void logout(View view) {
        SPUtils.setBooleanPreferences(this, SPUtils.ISLOGIN, false);
    }

    @Override
    public void loginPass() {
        startActivity(new Intent(this, HomeActivity.class));
    }
}

登录页LoginActivity

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    public void login(View view) {
        SPUtils.setBooleanPreferences(this, SPUtils.ISLOGIN, true);
        finish();
    }
}

主页面HomeActivity

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }
}

被代理对象接口ILogin

public interface ILogin {
   void loginPass();
}

代理对象MyHandler

public class MyHandler implements InvocationHandler {

   // 代理的真实对象
   private Object target;
   private Context context;

   public MyHandler(Object target, Context context) {
       this.target = target;
       this.context = context;
   }

   @Override
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
       if (SPUtils.getBooleanPreference(context, SPUtils.ISLOGIN, false)) {
           method.invoke(target, args);
       } else {
           context.startActivity(new Intent(context, LoginActivity.class));
       }
       return null;
   }
}

工具类SPUtils

public class SPUtils {

   private static final String NAME = "test";
   public static final String ISLOGIN = "isLogin";

   public static void setBooleanPreferences(Context context, String key ,boolean value) {
       SharedPreferences sharedPreferences = context.getSharedPreferences(
               NAME, Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putBoolean(key, value);
       editor.apply();
   }

   public static boolean getBooleanPreference(Context context, String key, Boolean defaultValue) {
       SharedPreferences sharedPreferences = context.getSharedPreferences(
               NAME, Context.MODE_PRIVATE);
       return sharedPreferences.getBoolean(key, defaultValue);
   }
}

布局文件
activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center"
   tools:context=".MainActivity">

   <TextView
       android:layout_width="match_parent"
       android:layout_height="35dp"
       android:background="#cccccc"
       android:textColor="#000000"
       android:text="登录验证"
       android:gravity="center"
       android:onClick="testLogin"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

   <TextView
       android:layout_width="match_parent"
       android:layout_height="35dp"
       android:layout_marginTop="10dp"
       android:background="#cccccc"
       android:textColor="#000000"
       android:text="退出登录"
       android:gravity="center"
       android:onClick="logout"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

activity_login

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center_vertical">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="用户名" />

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="密码" />
   <TextView
       android:layout_width="match_parent"
       android:layout_height="30dp"
       android:gravity="center"
       android:onClick="login"
       android:background="#cccccc"
       android:textColor="#000000"
       android:text="登录"/>

</LinearLayout>

activity_home

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="home"/>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值