It could possibly be better if you use Bluetooth API from your own Service
as the Activity
can be destroyed any time. I think this will take care of the problem, if any, because there will be only one Service
. The service could pass messages to activities using Handler
.
BluetoothAdapter causes activity leak
-
22-07-2023 - |
Вопрос
I have an activity that listens for a bluetooth headset because it plays audio. In the activity's onCreate() method, I register the listener via the adapter:
mBluetoothAdapter = BluetoothAdapter..getDefaultAdapter();
if (mBluetoothAdapter != null)
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mBluetoothProfileServiceListener,BluetoothProfile.HEADSET);
Then in onDestroy(), I close the proxy:
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
If I open and close the activity several times without connecting a bluetooth headset, all of the activities remain in memory. Using a memory dump, I can see that all of them are being held by a single reference from mServiceListener in android.bluetooth.BluetoothHeadset.
I can't find anything in the developer docs about how to remove the listener in order to release the activity. Is there any way to remove the activity as a listener so that the memory is released?
Решение
Не связан с StackOverflow