Android InApp账单:如何检测后台中断购买流程?(Android InApp Billing : How to detect back press interrupting the purc

编程入门 行业动态 更新时间:2024-10-24 20:12:07
Android InApp账单:如何检测后台中断购买流程?(Android InApp Billing : How to detect back press interrupting the purchase process?)

我正在尝试使用IabHelper实施inApp结算服务。 我设法在没有问题的情况下完成整个购买流程。

//----------------------------------------------- public void billingServiceLaunchPurchase(String item) { //----------------------------------------------- if (isNetworkAvailableSync(getBaseContext())) { currBuyItem=item; billingConsummeType=1; mHelper.launchPurchaseFlow(BaseActivity.this, item, 10001, mPurchaseFinishedListener, ""); } else { onBillingServiceFailed(); } } //----------------------------------------------- mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { //----------------------------------------------- public void onIabPurchaseFinished(IabResult result, Purchase purchase) { if (result.isFailure()) { // Handle error onBillingServiceFailed(); return; } else if (purchase.getSku().equals(currBuyItem)) { billingServiceConsumeItem(); } } }; @Override //----------------------------------------------------------------------- protected void onActivityResult(int requestCode, int resultCode, Intent data) //----------------------------------------------------------------------- { if (billingServiceConnected) { if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { super.onActivityResult(requestCode, resultCode, data); } else { // onActivityResult handled by IABUtil. } } else super.onActivityResult(requestCode, resultCode, data); }

但是,当用户启动购买时,我无法检测到该事件,但在Google确认屏幕上用“购买”按钮退格以中断该事件。

它既不会在onIabPurchaseFinished上触发失败,也不会触发onActivityResult,因此我的应用程序处于中间状态。

请帮我解决我的问题。

I'm trying to implement the inApp billing service with IabHelper. I manage to go through the full purchase process without problems.

//----------------------------------------------- public void billingServiceLaunchPurchase(String item) { //----------------------------------------------- if (isNetworkAvailableSync(getBaseContext())) { currBuyItem=item; billingConsummeType=1; mHelper.launchPurchaseFlow(BaseActivity.this, item, 10001, mPurchaseFinishedListener, ""); } else { onBillingServiceFailed(); } } //----------------------------------------------- mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { //----------------------------------------------- public void onIabPurchaseFinished(IabResult result, Purchase purchase) { if (result.isFailure()) { // Handle error onBillingServiceFailed(); return; } else if (purchase.getSku().equals(currBuyItem)) { billingServiceConsumeItem(); } } }; @Override //----------------------------------------------------------------------- protected void onActivityResult(int requestCode, int resultCode, Intent data) //----------------------------------------------------------------------- { if (billingServiceConnected) { if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { super.onActivityResult(requestCode, resultCode, data); } else { // onActivityResult handled by IABUtil. } } else super.onActivityResult(requestCode, resultCode, data); }

However, I cannot detect the event when the user launches the purchase but then press the backspace on the Google confirmation screen with the button "BUY" to interrupt it.

It neither triggers a failure on onIabPurchaseFinished nor it triggers onActivityResult so my application stays in an intermediary status.

Please help me to solve my problem.

最满意答案

根据我了解您的问题,您正在查找应用结算中的购买取消事件。

你可以通过onActivityResult()方法触发这个。在onActivityResult()方法的代码下面。 有简单的RESEULT_CANCEL类型,显示你的用户已取消购买。

if (mHelper == null) return; if (requestCode == 10001) { int responseCode = data.getIntExtra("RESPONSE_CODE", 0); String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); Log.d("INAPP_PURCHASE_DATA", ">>>" + purchaseData); String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); Log.d("INAPP_DATA_SIGNATURE", ">>>" + dataSignature); String continuationToken = data .getStringExtra("INAPP_CONTINUATION_TOKEN"); Log.d("INAPP_CONTINUATION_TOKEN", ">>>" + continuationToken); if (resultCode == RESULT_OK) { try { Log.d("purchaseData", ">>>"+purchaseData); JSONObject jo = new JSONObject(purchaseData); String sku = jo.getString("productId"); alert("You have bought the " + sku + ". Excellent choice, adventurer!"); } catch (JSONException e) { alert("Failed to parse purchase data."); e.printStackTrace(); } } else if (resultCode == RESULT_CANCELED) { // } else if (resultCode == RESULT_CANCELED) { Toast.makeText(AppMainTest.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show(); } else if (resultCode == IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) { Toast.makeText(AppMainTest.this, "Item already owned", Toast.LENGTH_SHORT).show(); } } }

要么

您还可以使用业务逻辑手动处理。 检查用户是否取消购买产品,然后将标志用户购买或者不购买,然后再次调用launchPurchaseFlow()方法。

编辑

onDestroy() method @Override public void onDestroy() { super.onDestroy(); // very important: Log.d(TAG, "Destroying helper."); if (mHelper != null) mHelper.dispose(); mHelper = null; }

如果你有按钮,那么你可以直接调用launchPurchaseFlow()方法到onClick事件中,这样每次你的mHelper创建为新购买。

要么

如果您在onCreate方法中使用它,并且您没有任何按钮点击事件来购买产品,则根据我的知识,您必须将值设为null。

希望它能解决你的问题。

According to what I have understood your question, you are searching for purchase cancel event in app billing.

You can trigger this via onActivityResult() method.Put below code in onActivityResult() method. There is simple RESEULT_CANCEL type that shows you user has been cancel purchasing.

if (mHelper == null) return; if (requestCode == 10001) { int responseCode = data.getIntExtra("RESPONSE_CODE", 0); String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); Log.d("INAPP_PURCHASE_DATA", ">>>" + purchaseData); String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); Log.d("INAPP_DATA_SIGNATURE", ">>>" + dataSignature); String continuationToken = data .getStringExtra("INAPP_CONTINUATION_TOKEN"); Log.d("INAPP_CONTINUATION_TOKEN", ">>>" + continuationToken); if (resultCode == RESULT_OK) { try { Log.d("purchaseData", ">>>"+purchaseData); JSONObject jo = new JSONObject(purchaseData); String sku = jo.getString("productId"); alert("You have bought the " + sku + ". Excellent choice, adventurer!"); } catch (JSONException e) { alert("Failed to parse purchase data."); e.printStackTrace(); } } else if (resultCode == RESULT_CANCELED) { Toast.makeText(AppMainTest.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show(); } else if (resultCode == IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) { Toast.makeText(AppMainTest.this, "Item already owned", Toast.LENGTH_SHORT).show(); } } }

or

you can also handle manually by using your business logic. check if user cancel purchase product then put flag user has been purchased or not if not then call launchPurchaseFlow() method again.

EDIT

onDestroy() method @Override public void onDestroy() { super.onDestroy(); // very important: Log.d(TAG, "Destroying helper."); if (mHelper != null) mHelper.dispose(); mHelper = null; }

if you have button then you can directly call launchPurchaseFlow() method into onClick event so that every time your mHelper created as new purchase.

or

if you are using it in onCreate method and you haven't any button click event to purchase product then you have to give value as null according to my knowledge.

Hope it will solve your problem.

更多推荐

本文发布于:2023-07-04 10:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:账单   后台   流程   Android   InApp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!