launchBillingFlow () не может отобразить экран покупки Google Play - PullRequest
0 голосов
/ 28 сентября 2019

Я использую Google Play Billing Library в своем приложении.Приложение реле в типе внутреннего теста реле.Но когда я нажал кнопку продукта, launchBillingFlow () не смог отобразить экран покупки Google Play. После щелчка код следует непосредственно за частью блока «// ЧТО-ТО И РАБОТАЕМ В МЕНЮ».Строки конфигурации моих приложений: во-первых, в моем манифесте есть «com.android.vending.BILLING».Во-вторых,мои файлы Gradle имеют текущую версию библиотеки биллинга;msgstr "реализация" com.android.billingclient: биллинг: 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) {

    }
}
...