launchBillingFlow()无法显示Google Play购买屏幕

时间:2019-09-28 17:09:53

标签: android in-app-billing android-billing play-billing-library

我在我的应用中使用Google Play结算库。内部测试发布类型中的应用发布。但是,当我单击产品按钮时,launchBillingFlow()无法显示Google Play购买屏幕。单击后,代码直接跟随该块的“ //执行某些操作并返回菜单操作”部分。 我的应用程序配置行:首先在清单中有一个“ com.android.vending.BILLING”。其次;我的gradle文件具有当前版本的计费库; “实施'com.android.billingclient:billing:2.0.3'”。但是我不知道为什么仍然无法显示购买屏幕。  我的代码如下;

 import android.content.Intent;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchaseHistoryResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.ynapp.yesornoapp.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyProductActivity extends AppCompatActivity  implements PurchasesUpdatedListener {
    //PREMIUM PRODUCTS
    Button btProduct;
    public static final String PR_PREFS_PRODUCT_ONE = "ProductOne";
    SharedPreferences prefs_product_one;
    //Will be used for in-app purchases.
    List<SkuDetails> skuDetailsList;
    BillingClient billingClient;
    Intent itMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        themeUtils.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_my_product);
        setupBillingClient();
        btProduct = (Button)findViewById(R.id.buttonforest);
    }

    public void onClickTheme(View view){
        SharedPreferences sharedPref = getSharedPreferences("MyData",MODE_PRIVATE);//MODE_PRIVATE
        final SharedPreferences.Editor editor = sharedPref.edit();
        switch (view.getId()){
            case R.id.buttonproductone:
                //önceden ödeme yapılıp yapılmadığını sorguluyoruz
                prefs_product_one= getSharedPreferences(PR_PREFS_PRODUCT_ONE,MODE_PRIVATE);
                final int product_one_flag = prefs_product_one.getInt("productone",0);
                List<String> skuList = new ArrayList<>();
                skuList.add("purch_product_one");

                if(billingClient.isReady() && product_one_flag==0){
                    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP).build();

                    billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                            if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK &&
                            skuDetailsList != null){
                                for(SkuDetails skuDetails: skuDetailsList){
                                    String sku = skuDetails.getSku();
                                    String price = skuDetails.getPrice();
                                    if("prefs_product_one".equals(sku)){
                                        Toast.makeText(MyProductActivity.this, sku + ": " + price, Toast.LENGTH_SHORT).show();
                                        BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                                                .setSkuDetails(skuDetails)
                                                .build();
                                        BillingResult billingResponseCode = billingClient.launchBillingFlow(MyProductActivity.this, flowParams);
                                        if (billingResponseCode.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                                            //DO SOMETHING AND RETURN MENU OPERATIONS
                                        }
                                    }
                                }
                            }

                        }

                    });
                }
                else{
                    if(product_one_flag ==0)
                        Toast.makeText(MyProductActivity.this, "Cannot query product", Toast.LENGTH_SHORT).show();
                    else{
                        Toast.makeText(getApplicationContext(),btProduct.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    public  void setupBillingClient(){
        billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                    Toast.makeText(MyProductActivity.this, "Success to connect Billing", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onBillingServiceDisconnected() {

            }
        });
    }


    @Override
    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {

    }
}

1 个答案:

答案 0 :(得分:0)

为什么在解析调用querySkuDetailsAsync的结果时开始购买?您应该在按钮的onClick事件或由用户的操作触发的事件中包含一个按钮和launchBillingFlow。

在querySkuDetailsAsync的回调中,您可以解析结果并获取一些SKU元数据来呈现您的付费专区。