最近在对接手环的第三方SDK,功能对接基本都是设置一个回调监听,然后调用对应的SDK方法,最后在回调方法中实现我们自己的逻辑处理。对接过程中有一个设备扫描激活页面,按照开发文档也就是调用SDK的某个方法(参数里面有一个回调监听),很简单。然而,在关闭这个页面之后,回调方法却还能被调用,之后LeakCanary也报了这个页面存在内存泄漏。进入SDK中查看了一下该方法,发现传入SDK的回调监听都会被置为static,从而一直持有调用Activity的引用,导致Activity无法被释放而产生内存泄漏。为了解决这个问题,刚开始写了一个静态内部类来解决,但后面发现几乎所有传入SDK的回调监听都会被置为static,所以只好对SDK调用方法统一再进行一层封装管理。
SDK调用方法统一管理
public class ClingManager {
private OnScanClingDeviceListener onScanClingDeviceListener;
private Handler mHandler;
private static class ClingManagerHolder {
public static final ClingManager INSTANCE = new ClingManager();
}
private ClingManager() {
mHandler = new Handler();
}
public static ClingManager getInstance() {
return ClingManagerHolder.INSTANCE;
}
/**
* 添加扫描设备监听
* @param listener
*/
public void addScanClingDeviceListener(OnScanClingDeviceListener listener) {
onScanClingDeviceListener = listener;
}
/**
* 移除扫描设备监听
*/
public void removeScanClingDeviceListener() {
onScanClingDeviceListener = null;
}
/**
* 扫描设备
*/
public void scanClingDevice() {
ClingSdk.startScan(10 * 60 * 1000, new OnBleListener.OnScanDeviceListener() {
@Override
public void onBleScanUpdated(Object o) {
//蓝牙连接成功后,不会再扫描
if (o == null) {
return;
}
final ArrayList<BluetoothDeviceInfo> arrayList = (ArrayList<BluetoothDeviceInfo>) o;
if (arrayList.size() > 0 && onScanClingDeviceListener != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
onScanClingDeviceListener.onScanClingDevice(arrayList);
}
});
}
}
});
}
/**
* 扫描设备监听
*/
public interface OnScanClingDeviceListener {
void onScanClingDevice(ArrayList<BluetoothDeviceInfo> clingDeviceList);
}
}
Activity中调用
需要注意的就是要在Activity的onDestroy生命周期方法中将扫描设备回调监听移除。
public class BindActivity {
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bind);
initDataCallBack();
}
@Override
protected void onStart() {
super.onStart();
registerReceiver(mBleReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
if (!BleUtil.isBleOpen()) {
showOpenBleMsg();
}
}
@Override
protected void onResume() {
super.onResume();
startScan();
}
private void showOpenBleMsg() {
Snackbar.make(ripple, "请打开蓝牙来绑定手环/手表", Snackbar.LENGTH_INDEFINITE)
.setAction("打开蓝牙", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(mIntent, REQUEST_OPEN_BLE);
}
})
.show();
}
private void initDataCallBack() {
// 扫描设备回调监听
ClingManager.getInstance().addScanClingDeviceListener(new ClingManager.OnScanClingDeviceListener() {
@Override
public void onScanClingDevice(ArrayList<BluetoothDeviceInfo> clingDeviceList) {
for (BluetoothDeviceInfo clingDevice : clingDeviceList) {
if (TextUtils.equals(clingDevice.getmBleDevice().getName(), "PBAND 9048")) {
ClingSdk.stopScan();
ClingManager.getInstance().registerClingDevice(clingDevice);
break;
}
}
}
private void startScan() {
ClingSdk.stopScan();
ClingSdk.setClingDeviceType(ClingSdk.CLING_DEVICE_TYPE_ALL);
ClingManager.getInstance().scanClingDevice();
}
private BroadcastReceiver mBleReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
switch (blueState) {
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_TURNING_ON:
break;
case BluetoothAdapter.STATE_ON:
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
startScan();
}
}, 1000);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
break;
default:
break;
}
}
};
@Override
protected void onPause () {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(mBleReceiver);
}
@Override
protected void onDestroy () {
super.onDestroy();
ClingManager.getInstance().removeScanClingDeviceListener();
}
}