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
<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
<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
<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>