1
我有一個活動,因爲它會播放音頻,因此會收聽藍牙耳機。在活動的onCreate()方法中,我通過適配器註冊監聽器:BluetoothAdapter導致活動泄漏
mBluetoothAdapter = BluetoothAdapter..getDefaultAdapter();
if (mBluetoothAdapter != null)
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mBluetoothProfileServiceListener,BluetoothProfile.HEADSET);
然後在的onDestroy(),我關閉代理:
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
如果我打開和關閉活動幾次如果沒有連接藍牙耳機,所有的活動都會留在內存中。使用內存轉儲,我可以看到它們都被android.bluetooth.BluetoothHeadset中的mServiceListener引用。
我無法找到有關如何刪除監聽器,以釋放該活動的開發者文檔任何東西。有沒有什麼方法可以刪除作爲偵聽器的活動,以便釋放內存?
我希望的方式來註銷監聽器,但它看起來這是它周圍的唯一途徑。我能夠將藍牙適配器和偵聽器移動到我的活動在onCreate中綁定的服務中,然後在onDestroy中取消綁定。這似乎已修復了活動泄漏。 –