Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序购买中谷歌播放计费库的AIDL_Java_Android_In App Subscription_Play Billing Library - Fatal编程技术网

Java 应用程序购买中谷歌播放计费库的AIDL

Java 应用程序购买中谷歌播放计费库的AIDL,java,android,in-app-subscription,play-billing-library,Java,Android,In App Subscription,Play Billing Library,我正在尝试将我在AIDL中的应用内购买迁移到新的计费库版本3,以满足新的google需求 在AIDL中,有一个base64EncodedPublicKey,我用来识别我的应用程序。计费库版本中是否有同等版本?我正在创建一个测试类,通过单击按钮打开支付流,但是下面的代码似乎不起作用。当我点击按钮时,什么也没有发生 public class MainActivity extends AppCompatActivity implements PurchasesUpdatedListener { pr

我正在尝试将我在AIDL中的应用内购买迁移到新的计费库版本3,以满足新的google需求

在AIDL中,有一个
base64EncodedPublicKey
,我用来识别我的应用程序。计费库版本中是否有同等版本?我正在创建一个测试类,通过单击按钮打开支付流,但是下面的代码似乎不起作用。当我点击按钮时,什么也没有发生

 public class MainActivity extends AppCompatActivity implements PurchasesUpdatedListener {
private Button buyButton;
private BillingClient billingClient;

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

    buyButton = (Button) findViewById(R.id.buy);

    billingClient = BillingClient.newBuilder(this)
            .setListener(this)
            .enablePendingPurchases()
            .build();

    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(BillingResult billingResult) {
            if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
                // The BillingClient is ready. You can query purchases here.
                loadAllSKUs();
            }
        }
        @Override
        public void onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    });

}

@Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
            && purchases != null) {
        for (Purchase purchase : purchases) {
            handlePurchase(purchase);
        }
    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
    } else {
        // Handle any other error codes.
    }
}

public void handlePurchase(Purchase purchase) {
    if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
        if(!purchase.isAcknowledged())
        {
            AcknowledgePurchaseParams acknowledgePurchaseParams
                    = AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .build();

            billingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
                @Override
                public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                    if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
                    }
                }
            });
        }
    }
}


private void loadAllSKUs() {
    if (billingClient.isReady()) {
        List<String> skuList = new ArrayList<>();
        skuList.add("4");
        SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
        params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);
        billingClient.querySkuDetailsAsync(params.build(),
                new SkuDetailsResponseListener() {
                    @Override
                    public void onSkuDetailsResponse(BillingResult billingResult,
                                                     List<SkuDetails> skuDetailsList) {
                        Log.d("Testing", "testing2");
                        Log.d("Testing", String.valueOf(skuDetailsList.size()));
                        for (SkuDetails skuDetails : skuDetailsList) {
                            if (skuDetails.getSku() == "4")
                                //if (skuDetails.getSku() == "4")
                                buyButton.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View v) {
                                        //DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
                                        BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                                .setSkuDetails(skuDetails)
                                                .build();
                                        int responseCode = billingClient.launchBillingFlow(MainActivity.this, billingFlowParams).getResponseCode();
                                    }
                                });
                        }
                    }
                });
    } else {
        Log.d("Testing", "not ready");
    }
}
}
公共类MainActivity扩展AppCompative实现PurchasesUpdatedListener{
私人按钮购买按钮;
私人账单客户账单客户;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buyButton=(按钮)findViewById(R.id.buy);
billingClient=billingClient.newBuilder(此)
.setListener(此)
.enablePendingPurchases()
.build();
startConnection(新的BillingClientStateListener()){
@凌驾
BillingSetupFinished(BillingResult BillingResult)上的公共无效{
if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK){
//BillingClient已准备就绪。您可以在此查询购买情况。
loadAllSKUs();
}
}
@凌驾
公共无效onBillingServiceDisconnected(){
//尝试在下一次请求时重新启动连接以
//Google通过调用startConnection()方法播放。
}
});
}
@凌驾
已暂停购买的公共作废(BillingResult BillingResult,列表购买){
如果(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK
&&购买!=空){
用于(采购:采购){
手工采购(采购);
}
}else if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.USER\u已取消){
//处理由用户取消采购流程引起的错误。
}否则{
//处理任何其他错误代码。
}
}
公共无效手购(购买){
if(purchase.getPurchaseState()==purchase.PurchaseState.PURCHASED){
如果(!purchase.isAcknowledged())
{
确认采购计划确认采购计划
=AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.AcknowledgepPurchase(AcknowledgepPurchaseParams,新AcknowledgepPurchaseResponseListener(){
@凌驾
确认采购响应(BillingResult BillingResult)上的公共无效{
if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK){
}
}
});
}
}
}
私有void loadAllSKUs(){
if(billingClient.isReady()){
List skuList=new ArrayList();
skuList。添加(“4”);
SkuDetailsParams.Builder params=SkuDetailsParams.newBuilder();
参数setskulist(skuList).setType(BillingClient.SkuType.SUBS);
billingClient.querySkuDetailsAsync(params.build(),
新SkuDetailsResponseListener(){
@凌驾
公共无效详细信息响应(BillingResult BillingResult,
列表(详细信息列表){
日志d(“测试”、“测试2”);
Log.d(“测试”,String.valueOf(skudailslist.size());
用于(SkuDetails SkuDetails:skuDetailsList){
if(skuDetails.getSku()=“4”)
//if(skuDetails.getSku()=“4”)
buyButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//做点什么!{运行一些函数…做检查…等等}
BillingFlowParams BillingFlowParams=BillingFlowParams.newBuilder()
.详细信息(SKU详细信息)
.build();
int responseCode=billingClient.launchBillingFlow(MainActivity.this,billingFlowParams).getResponseCode();
}
});
}
}
});
}否则{
日志d(“测试”,“未准备就绪”);
}
}
}
下面是xml文件

  <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/buy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 </androidx.constraintlayout.widget.ConstraintLayout>