我正在使用ActivityResult打开一个Activity,并且在成功购买项目之后,我正在关闭当前活动,该活动包含清除过程并将数据发回。但泄漏金丝雀捕获关于BillingBroadcastReceiver的内存泄漏。我初始计费客户OnCreate
并发布onDestroy
。
这是我在OnCreate
中调用的init方法
billingClient = BillingClient.newBuilder(this).setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(int responseCode) {
if (responseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
loadProducts();
} else {
// Error
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
Timber.d("Connection Error");
}
});
当billingClient
准备就绪时加载产品信息
private void loadProducts() {
if (billingClient.isReady()) {
List<String> skuList = new ArrayList<>(getViewModel().getSkuIdList());
SkuDetailsParams params = SkuDetailsParams.newBuilder().setSkusList(skuList).setType(BillingClient.SkuType.INAPP).build();
billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
if (responseCode == BillingClient.BillingResponse.OK) {
Timber.d("SkuList --> %s", skuDetailsList.size());
} else {
Timber.d("Can't querySkuDetailsAsync, responseCode: %s", responseCode);
}
}
});
} else {
Timber.d("Billing Client not Ready");
}
}
这是我在OnDestroy
中调用的释放方法
if (billingClient != null && billingClient.isReady()) {
billingClient.endConnection();
billingClient = null;
}
OnPurchaseUpdated我进行了服务调用并根据服务结果关闭此活动。
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
for (Purchase purchase : purchases) {
billingClient.consumeAsync(purchase.getPurchaseToken(), new ConsumeResponseListener() {
@Override
public void onConsumeResponse(int responseCode, String purchaseToken) {
if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
Timber.d("onConsumeResponse --> %s", purchaseToken);
getViewModel().informPurchase(necessary data);
}
}
});
}
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
// Handle an error caused by a user canceling the purchase flow.
Timber.d("Billing Cancelled");
} else {
Timber.d("An Error Occured");
}
}
我正在使用最新的库来购买应用程序
implementation 'com.android.billingclient:billing:1.2.1'
成功购买商品并关闭最近的活动后,Leak Canary向我显示了此错误。我怎样才能避免这种内存泄漏?
在billingClient
准备就绪之前,活动正在被销毁,所以billingClient.isReady
将是假的,并且endConnection
将永远不会被调用,因此对Activity的引用(Activity是BillingClient的监听器)将不会被删除并且你将有泄漏。
要修复,只需:
if (billingClient != null) {
billingClient.endConnection();
billingClient = null;
}