Skip to content

Commit

Permalink
优化框架内存,优化代码。修复了订阅之后无法注销的BUG
Browse files Browse the repository at this point in the history
优化框架内存,优化代码。修复了订阅之后无法注销的BUG
  • Loading branch information
LiqiNew committed Oct 14, 2017
1 parent 165c667 commit 23eee36
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## HandlerFrame框架
HandlerFrame是一个基于观察者模式实现跨界面Handler通讯框架。只要在HandlerFrame里面订阅过的对象,在任何界面都可以发送信息去跟订阅对象通讯。
HandlerFrame是一个基于观察者模式采用信息分发机制去实现跨界面Handler通讯框架。只要在HandlerFrame里面订阅过的对象,在任何界面都可以发送信息去跟订阅对象通讯。
且整个项目里面只存在一个Handler实例对象,不用担心因为Handler对象造成的内存问题。
#### HandlerFrame框架使用简介
```java
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.liqi"
minSdkVersion 10
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/liqi/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.v4.app.FragmentActivity;

import com.liqi.handler.BaseHandlerOperate;
import com.liqi.handler.OnBaseHandlerOperateListener;
import com.liqi.handler.OnBaseHandlerUpDateListener;

/**
Expand All @@ -17,7 +18,7 @@
*/
public abstract class BaseActivity extends FragmentActivity implements
OnBaseHandlerUpDateListener {
protected BaseHandlerOperate handler;
protected OnBaseHandlerOperateListener handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/liqi/TestOneActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ protected void onCreateData(Bundle savedInstanceState) {
test_button02.setOnClickListener(this);
test_textview_new = (TextView) findViewById(R.id.test_textview_new);
handler.onSubscribe(TestOneActivity.class,this);
findViewById(R.id.test_button03).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
handler.onSubscribe(TestOneActivity.class,TestOneActivity.this);
}
});
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/liqi/TestTwoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ protected void onCreateData(Bundle savedInstanceState) {
test_button01.setOnClickListener(this);
test_button02 = (Button) findViewById(R.id.test_button02);
test_button02.setOnClickListener(this);
findViewById(R.id.test_button03).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
handler.removeSubscribe(TestOneActivity.class);
}
});
}

@Override
protected void onDestroyData() {
handler.removeSubscribe(TestOneActivity.class);

}

@Override
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/layout/test_one_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,12 @@
android:text="跨页面发送信息给“首界面”"
android:textColor="@android:color/black"
android:textSize="15sp" />

<Button
android:id="@+id/test_button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="“第二个界面”订阅"
android:textColor="@android:color/black"
android:textSize="15sp" />
</LinearLayout>
9 changes: 8 additions & 1 deletion app/src/main/res/layout/test_two_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@
android:text="跨页面发送信息给“第二个页面”"
android:textColor="@android:color/black"
android:textSize="15sp" />

<Button
android:id="@+id/test_button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="注销“第二个页面”订阅"
android:textColor="@android:color/black"
android:textSize="15sp" />
</LinearLayout>
2 changes: 1 addition & 1 deletion handlerFrame/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 10
Expand Down
258 changes: 127 additions & 131 deletions handlerFrame/src/main/java/com/liqi/handler/BaseHandler.java
Original file line number Diff line number Diff line change
@@ -1,152 +1,148 @@
package com.liqi.handler;

import java.lang.ref.SoftReference;

import android.os.Handler;
import android.os.Message;
import android.util.SparseArray;

import java.lang.ref.SoftReference;

/**
* 用集合存储软引用调用的handler(核心)
*
*
* @author Liqi
*
*/
public class BaseHandler extends Handler implements OnBaseHandlerMethodListener {
private static BaseHandler baseHandler;
private BaseHandlerGetKey baseHandlerGetKey;
private SparseArray<SoftReference<OnBaseHandlerUpDateListener>> arrayReference;
private OnFactoryOperateListener mOnFactoryOperateListener;
class BaseHandler extends Handler implements OnBaseHandlerMethodListener {
private static BaseHandler baseHandler;
private final SparseArray<SoftReference<OnBaseHandlerUpDateListener>> arrayReference;
private OnBaseHandlerGetKeyListener mOnBaseHandlerGetKeyListener;
private OnFactoryOperateListener mOnFactoryOperateListener;

@Override
public void setOnFactoryOperateListener(
OnFactoryOperateListener onFactoryOperateListener) {
this.mOnFactoryOperateListener = onFactoryOperateListener;
}
private BaseHandler() {
arrayReference = new SparseArray<>();
}

/**
* 获取handler对象
*
* @return
*/
public synchronized static BaseHandler getBaseHandler() {
synchronized (BaseHandler.class.getName()) {
if (baseHandler == null) {
baseHandler = new BaseHandler();
}
}
return baseHandler;
}
/**
* 获取handler对象
*
* @return
*/
synchronized static BaseHandler getBaseHandler() {
synchronized (BaseHandler.class.getName()) {
if (baseHandler == null) {
baseHandler = new BaseHandler();
}
}
return baseHandler;
}

private BaseHandler() {
if (null == arrayReference) {
arrayReference = new SparseArray<>();
}
}
@Override
public void setOnFactoryOperateListener(
OnFactoryOperateListener onFactoryOperateListener) {
this.mOnFactoryOperateListener = onFactoryOperateListener;
}

@Override
public void handleMessage(Message msg) {
if (null != arrayReference && arrayReference.size() > 0) {
if (null != baseHandlerGetKey) {
SoftReference<OnBaseHandlerUpDateListener> reference = arrayReference
.get(baseHandlerGetKey.handlerGetKey());
if (null != reference) {
OnBaseHandlerUpDateListener baseActivity = reference.get();
if (null != baseActivity) {
baseActivity.handleMessage(msg);
}
}
} else {
System.out.println("handleMessage>>>>handler获取Key接口为空");
}
}
}
@Override
public void handleMessage(Message msg) {
if (arrayReference.size() > 0) {
if (null != mOnBaseHandlerGetKeyListener) {
SoftReference<OnBaseHandlerUpDateListener> reference = arrayReference
.get(mOnBaseHandlerGetKeyListener.handlerGetKey());
if (null != reference) {
OnBaseHandlerUpDateListener baseActivity = reference.get();
if (null != baseActivity) {
baseActivity.handleMessage(msg);
}
}
} else {
System.out.println("handleMessage>>>>handler获取Key接口为空");
}
}
}

/**
* 把传进来的对象通过塞入软引用添加进SparseArray集合里面
*
* @param activity
*/
@Override
public void addSparseArray(OnBaseHandlerUpDateListener activity) {
if (null != baseHandlerGetKey) {
arrayReference.put(baseHandlerGetKey.handlerGetKey(),
new SoftReference<>(activity));
} else {
System.out.println("addSparseArray>>>handler获取Key接口为空");
}
}
/**
* 把传进来的对象通过塞入软引用添加进SparseArray集合里面
*
* @param activity
*/
@Override
public void addSparseArray(OnBaseHandlerUpDateListener activity) {
if (null != mOnBaseHandlerGetKeyListener) {
arrayReference.put(mOnBaseHandlerGetKeyListener.handlerGetKey(),
new SoftReference<>(activity));
} else {
System.out.println("addSparseArray>>>handler获取Key接口为空");
}
}

/**
* 发送message信息个handler
*
* @param tag
* @param obj
*/
@Override
public void putMessage(int tag, Object obj) {
Message message = this.obtainMessage();
message.what = tag;
message.obj = obj;
this.sendMessage(message);
}
/**
* 发送message信息个handler
*
* @param tag
* @param obj
*/
@Override
public void putMessage(int tag, Object obj) {
Message message = this.obtainMessage();
message.what = tag;
message.obj = obj;
this.sendMessage(message);
}

/**
* 清除SparseArray集合中指定键的软引用的数据 (!--Key是通过接口获取的,只需要调用此方法即可)
*/
@Override
public void removeKeyData() {
if (null != arrayReference && arrayReference.size() > 0) {
if (null != baseHandlerGetKey) {
int handlerGetKey = baseHandlerGetKey.handlerGetKey();
SoftReference<OnBaseHandlerUpDateListener> reference = arrayReference
.get(handlerGetKey);
if (null != reference) {
reference.clear();
arrayReference.remove(handlerGetKey);
if (mOnFactoryOperateListener != null) {
mOnFactoryOperateListener.removeFactoryKeyData(handlerGetKey);
}
}
} else {
System.out.println("removeKeyData>>>handler获取Key接口为空");
}
}
}
/**
* 清除SparseArray集合中指定键的软引用的数据 (!--Key是通过接口获取的,只需要调用此方法即可)
*/
@Override
public void removeKeyData() {
if (arrayReference.size() > 0) {
if (null != mOnBaseHandlerGetKeyListener) {
int handlerGetKey = mOnBaseHandlerGetKeyListener.handlerGetKey();
SoftReference<OnBaseHandlerUpDateListener> reference = arrayReference
.get(handlerGetKey);
if (null != reference) {
reference.clear();
arrayReference.remove(handlerGetKey);
if (mOnFactoryOperateListener != null) {
mOnFactoryOperateListener.removeFactoryKeyData(handlerGetKey);
}
}
} else {
System.out.println("removeKeyData>>>handler获取Key接口为空");
}
}
}

/**
* 清除SparseArray集合里面所有值
*/
@Override
public void removeAll() {
if (null != arrayReference && arrayReference.size() > 0) {
for (int i = 0; i < arrayReference.size(); i++) {
arrayReference.valueAt(i).clear();
}
arrayReference.clear();
if (mOnFactoryOperateListener != null) {
mOnFactoryOperateListener.removeAllFactoryData();
}
}
}
/**
* 清除SparseArray集合里面所有值
*/
@Override
public void removeAll() {
if (arrayReference.size() > 0) {
for (int i = 0; i < arrayReference.size(); i++) {
arrayReference.valueAt(i).clear();
}
arrayReference.clear();
if (mOnFactoryOperateListener != null) {
mOnFactoryOperateListener.removeAllFactoryData();
}
}
}

@Override
public void setBaseHandlerGetKey(BaseHandlerGetKey baseHandlerGetKey) {
this.baseHandlerGetKey = baseHandlerGetKey;
}
@Override
public void setOnBaseHandlerGetKeyListener(OnBaseHandlerGetKeyListener onBaseHandlerGetKeyListener) {
this.mOnBaseHandlerGetKeyListener = onBaseHandlerGetKeyListener;
}

/**
* Handler中SparseArray集合Key设置接口
*
* @author Liqi
*
*/
public interface BaseHandlerGetKey {
/**
* 设置SparseArray集合存入的Key
*
* @return
*/
int handlerGetKey();
}
/**
* Handler中SparseArray集合Key设置接口
*
* @author Liqi
*/
interface OnBaseHandlerGetKeyListener {
/**
* 设置SparseArray集合存入的Key
*
* @return
*/
int handlerGetKey();
}
}
Loading

0 comments on commit 23eee36

Please sign in to comment.