ysf*_*yln 11 android memory-leaks in-app-billing
我正在使用 ActivityResult 打开一个活动,在成功购买商品后,我将关闭当前的活动,该活动包含购买过程并将数据发回。但是 Leak Canary 捕获了有关 BillingBroadcastReceiver 的内存泄漏。我初始化计费客户端OnCreate并发布onDestroy。
这是我调用的 init 方法 OnCreate
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");
}
});
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我调用的释放方法 OnDestroy
if (billingClient != null && billingClient.isReady()) {
billingClient.endConnection();
billingClient = null;
}
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用最新的库进行应用内购买
implementation 'com.android.billingclient:billing:1.2.1'
Run Code Online (Sandbox Code Playgroud)
成功购买商品并关闭最近的活动后,Leak Canary 会向我显示此错误。如何避免这种内存泄漏?
Activity 在billingClient准备好之前就被销毁,因此
billingClient.isReady将是 false 并且endConnection永远不会被调用,因此对 Activity 的引用(Activity 是 BillingClient 的侦听器)不会被删除,并且您将发生泄漏。
要修复,只需执行以下操作:
if (billingClient != null) {
billingClient.endConnection();
billingClient = null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |