简体   繁体   中英

In-App Billing, show localized price on text

In my android app, I want to display the localized price, for an In-App purchase, on a button. I've tried following a guide to set up the In-App Billing ( https://medium.com/@patpatchpatrick/adding-the-google-play-billing-library-to-your-application-fbeb9ec03151 ) to set up the billing itself, and it seems to work on a test account.
Although .setSku and .setType is now deprecated, and .setSkuDetails is now to be used, which from documentation is great, as there's plenty of options.
However i can't seem to get any access to the SkuDetails class..

For a couple of weeks I've tried implementing In-App Billing, and looked at various articles and guides, but can't seem to find my way about it. I feel like i've tried everything and doesn't know where to turn next.

public class InAppBilling extends AppCompatActivity implements 
PurchasesUpdatedListener {

private static final String TAG = "InAppBilling";

//In APP Produkter
static final String ITEM_SKU_ADREMOVAL = "remove_ads_salary1";

private Button mButton;
private Button back_Button;
private String mAdRemovalPrice;
private SharedPreferences mSharedPreferences;

private BillingClient mBillingClient;



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


    mBillingClient = BillingClient.newBuilder(InAppBilling.this).setListener(this).build();
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(int responseCode) {
            if (responseCode == BillingClient.BillingResponse.OK){
                List skuList = new ArrayList<>();
                skuList.add(ITEM_SKU_ADREMOVAL);
                SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                mBillingClient.querySkuDetailsAsync(params.build(),
                        new SkuDetailsResponseListener() {
                            @Override
                            public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                                //Processing the response if the code = OK, and skuDetailsList isn't = null(empty)
                                if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null){
                                    for (SkuDetails skuDetails : skuDetailsList){
                                        String sku = skuDetails.getSku();
                                        String price = skuDetails.getPrice();
                                        if (ITEM_SKU_ADREMOVAL.equals(sku)){
                                            mAdRemovalPrice = price;
                                        }
                                    }
                                }
                            }
                        });
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            // IMPLEMENT RETRY POLICY - TRY TO RESTART ON NEXT REQUEST BY CALLING startConnection()
        }
    });

    mButton = findViewById(R.id.buy_button);
    mButton.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View view){
           BillingFlowParams flowParams = BillingFlowParams.newBuilder()

                 //THIS skuDetails gives the error 'Cannot resolve symbol
                   .setSkuDetails(skuDetails)
                  // .setSku(ITEM_SKU_ADREMOVAL)
                  // .setType(BillingClient.SkuType.INAPP)
                   .build();
           int responseCode = mBillingClient.launchBillingFlow(InAppBilling.this, flowParams);
       }
    });

    back_Button = findViewById(R.id.back_button);
    back_Button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //Returnere til det fragment man kom fra
            InAppBilling.super.onBackPressed();
        }

    });


}

Well, I'd love to gain access over SkuDetails, so I can use it's method getPrice(), to show localized prices, for my in-app.
Right now, I can't use getPrice().

Make your activity to implement the listeners, then you will be able to access everything more easily.

I reformatted your code to implement all your listeners, and it may have some minor mistakes, but I hope you get the idea.

Notice that I've also implemented the View.OnClickListener which makes the code more clear, yet you will need to assign at module level a variable for the skuDetails being passed to the builder, which I didn't do. Let me know if you have any questions.

public class InAppBilling extends AppCompatActivity implements
        PurchasesUpdatedListener, SkuDetailsResponseListener,
        BillingClientStateListener, View.OnClickListener
{

    private static final String TAG = "InAppBilling";

    //In APP Produkter
    static final String ITEM_SKU_ADREMOVAL = "remove_ads_salary1";

    private Button            mButton;
    private Button            back_Button;
    private String            mAdRemovalPrice;
    private SharedPreferences mSharedPreferences;

    private BillingClient mBillingClient;

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

        mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
        mBillingClient.startConnection(this);

        findViewById(R.id.buy_button).setOnClickListener(this);
        findViewById(R.id.back_button).setOnClickListener(this);
    }

    @Override
    public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases)
    {

    }

    @Override
    public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList)
    {
        if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null)
        {
            for (SkuDetails skuDetails : skuDetailsList)
            {
                String sku   = skuDetails.getSku();
                String price = skuDetails.getPrice();

                if (ITEM_SKU_ADREMOVAL.equals(sku))
                {
                    mAdRemovalPrice = price;
                }
            }
        }
    }

    @Override
    public void onClick(View view)
    {
        if (view.getId() == R.id.back_button)
        {
            super.onBackPressed();
        }
        else if (view.getId() == R.id.buy_button)
        {
            BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                    .setSkuDetails(skuDetails)
                    .build();

            int responseCode = mBillingClient.launchBillingFlow(this, flowParams);
        }
    }

    @Override
    public void onBillingSetupFinished(int responseCode)
    {
        if (responseCode == BillingClient.BillingResponse.OK)
        {
            List skuList = new ArrayList<>();
            skuList.add(ITEM_SKU_ADREMOVAL);
            SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
            params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
            mBillingClient.querySkuDetailsAsync(params.build(), this);
        }
    }

    @Override
    public void onBillingServiceDisconnected()
    {
        // IMPLEMENT RETRY POLICY - TRY TO RESTART ON NEXT REQUEST BY CALLING startConnection()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM