首頁 >Java >Android Studio Java - 應用程式內購買錯誤/問題

Android Studio Java - 應用程式內購買錯誤/問題

王林
王林轉載
2024-02-08 22:45:32665瀏覽

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刪除