首页 > Java > Android Studio Java - 应用内购买错误/问题

Android Studio Java - 应用内购买错误/问题

王林
发布: 2024-02-08 22:45:32
转载
682 人浏览过

php小编百草为你提供解决Android Studio Java应用内购买错误和问题的方法。在开发应用时,应用内购买是一项常见的功能,但很多开发者在实现时可能会遇到各种问题。本文将分享解决应用内购买错误和问题的有效方法,帮助你顺利完成应用的开发和发布。无论是遇到商品无法购买、购买流程中断还是支付验证失败等问题,我们都将给出详细的解决方案,帮助你快速解决问题,提升应用的用户体验。

问题内容

在我的应用程序中,我使用此库添加应用程序内购买。

这就是我的活动代码:

public class InfoController extends AppCompatActivity {

    private static final String PREFS_NAME = "TEST_MyPrefsFile";

    private static final String FIRST_TIME_KEY = "TEST_isFirstLaunch";

    private static final String IS_PRO_KEY = "TEST_isPro";
    private boolean isPro() {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return preferences.getBoolean(IS_PRO_KEY, false);
    }

    private void setProUser(boolean isPro) {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(IS_PRO_KEY, isPro);
        editor.apply();
    }

    private Button upgradeButton;

    String base64String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    private BillingConnector billingConnector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

        upgradeButton = findViewById(R.id.upgradeButton);

        if (!isPro()) {
            upgradeButton.setText("Click here to upgrade to Pro");
            initializeBillingClient();
        } else {
            upgradeButton.setEnabled(false);
            upgradeButton.setText("User is Pro");
        }
    }


    private void initializeBillingClient() {
        List<String> nonConsumableIds = new ArrayList<>();
        nonConsumableIds.add("com.xxxx.pro");  // Replace with your actual non-consumable product ID

        billingConnector = new BillingConnector(this, base64String)
                .setNonConsumableIds(nonConsumableIds)
                .autoAcknowledge()
                .enableLogging()
                .connect();

        billingConnector.setBillingEventListener(new BillingEventListener() {
            @Override
            public void onProductsFetched(@NonNull List<ProductInfo> productDetails) {

            }

            //this IS the listener in which we can restore previous purchases.
            // Code will execute on InfoController.java appear. If pro is already purchased, unlock pro features.
            @Override
            public void onPurchasedProductsFetched(@NonNull ProductType productType, @NonNull List<PurchaseInfo> purchases) {
                String purchasedProduct;
                boolean isAcknowledged;

                for (PurchaseInfo purchaseInfo : purchases) {
                    purchasedProduct = purchaseInfo.getProduct();
                    isAcknowledged = purchaseInfo.isAcknowledged();

                    if (!isPro()) {
                        if (purchasedProduct.equalsIgnoreCase("com.xxxx.pro")) {
                            if (isAcknowledged) {

                                // CustomToast.makeText(InfoController.this, "The previous purchase was successfully restored.", Toast.LENGTH_SHORT).show();

                                // setProUser(true);

                                Toast.makeText(InfoController.this, "isAcknowledged", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            }

            //this IS NOT the listener in which we'll give user entitlement for purchases (see ReadMe.md why)
            @Override
            public void onProductsPurchased(@NonNull List<PurchaseInfo> purchases) {

            }

            //this IS the listener in which we'll give user entitlement for purchases (the ReadMe.md explains why)
            @Override
            public void onPurchaseAcknowledged(@NonNull PurchaseInfo purchase) {
                String acknowledgedProduct = purchase.getProduct();

                if (acknowledgedProduct.equalsIgnoreCase("com.xxxx.pro")) {

                    Toast.makeText(InfoController.this, "The purchase was successfully made.", Toast.LENGTH_SHORT).show();

                    setProUser(true);

                    upgradeButton.setEnabled(false);
                    upgradeButton.setText("User is Pro");
                }
            }

            @Override
            public void onPurchaseConsumed(@NonNull PurchaseInfo purchase) {

            }

            @Override
            public void onBillingError(@NonNull BillingConnector billingConnector, @NonNull BillingResponse response) {
                switch (response.getErrorType()) {
                    case ACKNOWLEDGE_WARNING:
                        //this response will be triggered when the purchase is still PENDING
                        CustomToast.makeText(InfoController.this, "The transaction is still pending. Please come back later to receive the purchase!", Toast.LENGTH_SHORT).show();
                        break;
                    case BILLING_UNAVAILABLE:
                    case SERVICE_UNAVAILABLE:
                        CustomToast.makeText(InfoController.this, "Billing is unavailable at the moment. Check your internet connection!", Toast.LENGTH_SHORT).show();
                        break;
                    case ERROR:
                        CustomToast.makeText(InfoController.this, "Something happened, the transaction was canceled!", Toast.LENGTH_SHORT).show();
                        break;
                    case ITEM_ALREADY_OWNED:
                        Toast.makeText(InfoController.this, "The purchase has already been made.", Toast.LENGTH_SHORT).show();
                        setProUser(true);
                        upgradeButton.setEnabled(false);
                        upgradeButton.setText("User is Pro");
                            break;
                    case ITEM_NOT_OWNED:
                        //TODO - failure to consume since item is not owned
                        break;
                }
            }
        });
    }

    public void onUpgradeButtonClick(View view) {
        billingConnector.purchase(InfoController.this, "com.xxxx.pro");
    }
}
登录后复制

当我在应用程序购买中进行测试时,我使用促销代码(因为我的帐户没有连接卡)。

因此,当我打开此活动时,什么也没有显示。我按upgradebutton,弹出升级提示,我选择促销代码并输入促销代码,单击下一步,它告诉我价格为0,我按确认。此后,购买提示窗口将关闭,没有任何反应。没有显示 toast 消息,升级按钮文本仍然显示:click here to upgrade to pro

如果我现在关闭应用程序,重新打开它并转到 infocontroller(此活动),则会显示 toast 消息 isacknowledged

所以我的问题是:为什么购买完成后没有任何反应,但是当我购买后再次打开活动时,它显示isacknowledged

解决方法

处理前检查确认状态:

@override
public void onpurchasedproductsfetched(@nonnull producttype producttype, @nonnull list<purchaseinfo> purchases) {
    for (purchaseinfo purchaseinfo : purchases) {
        string purchasedproduct = purchaseinfo.getproduct();
        if (!ispro() && purchasedproduct.equalsignorecase("com.xxxx.pro")) {
            if (!billingconnector.ispurchaseditemacknowledged(purchaseinfo)) {
                // here is the code of purchase processing
            }
        }
    }
}
登录后复制

还将确认逻辑从 onpurchaseacknowledged 方法移至 onpurchasedproductsfetched 方法。这可能会有所帮助。

@Override
public void onPurchasedProductsFetched(@NonNull ProductType productType, @NonNull List<PurchaseInfo> purchases) {
    for (PurchaseInfo purchaseInfo : purchases) {
        String purchasedProduct = purchaseInfo.getProduct();
        if (!isPro() && purchasedProduct.equalsIgnoreCase("com.xxxx.pro")) {
            // here is the code of purchase processing
            handlePurchase(purchaseInfo);
        }
    }
}

private void handlePurchase(PurchaseInfo purchaseInfo) {
    String acknowledgedProduct = purchaseInfo.getProduct();
    if (acknowledgedProduct.equalsIgnoreCase("com.xxxx.pro")) {
        Toast.makeText(InfoController.this, "The purchase was successfully made.", Toast.LENGTH_SHORT).show();
        setProUser(true);
        upgradeButton.setEnabled(false);
        upgradeButton.setText("User is Pro");
    }
}
登录后复制

以上是Android Studio Java - 应用内购买错误/问题的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板