Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
限制Android订阅的功能_Android_In App Purchase_Billing_In App Subscription - Fatal编程技术网

限制Android订阅的功能

限制Android订阅的功能,android,in-app-purchase,billing,in-app-subscription,Android,In App Purchase,Billing,In App Subscription,我正在尝试为android应用程序创建一个应用内账单,允许用户购买订阅。我已经做到了这一点,我可以购买订阅,但我没有得到的是,我如何才能将应用程序中的某些功能限制到那些尚未订阅的用户 我似乎找不到任何关于这方面的教程。我想要的是在一个按钮按下,如果用户还没有订阅一个应用程序内计费窗口提示。我可以用这段代码实现这一点 public void launchBillingFLow(@SkuType String skuType, String productId) { Runnable la

我正在尝试为android应用程序创建一个应用内账单,允许用户购买订阅。我已经做到了这一点,我可以购买订阅,但我没有得到的是,我如何才能将应用程序中的某些功能限制到那些尚未订阅的用户

我似乎找不到任何关于这方面的教程。我想要的是在一个按钮按下,如果用户还没有订阅一个应用程序内计费窗口提示。我可以用这段代码实现这一点

public void launchBillingFLow(@SkuType String skuType, String productId) {

    Runnable launchBillingRequest = () -> {

        BillingFlowParams mBillingFlowParams;

        mBillingFlowParams = BillingFlowParams.newBuilder()
                .setSku(productId)
                .setType(skuType)
                .build();

        mBillingClient.launchBillingFlow((Activity) context, mBillingFlowParams);

    };

    executeServiceRequest(launchBillingRequest);

}

但若用户已经订阅了呢?所以问题是,我如何检查用户是否已订阅并执行按钮按下,如果未显示计费窗口。只有当用户连接到Internet时,我才能获得该信息吗?我需要在设备上存储该信息吗?

使用此订阅方法获取购买详细信息-

  • 检查订阅是否已购买(responseCode==7)
  • 使用purchasesResult.getPurchasesList()获取所有puschase列表
  • 使用onPurchasesUpdated方法在订阅的分阶段成功后立即获取响应
  • 使用BillingFlowParams.Builder显示应用内计费流程

        mBillingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
    
                    if (billingResponseCode == BillingClient.BillingResponse.OK) {
                        // The billing client is ready. You can query purchases here.
    
                        final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
                        for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
                            if (sourcePurchase != null) {
    
                                ConsumeResponseListener listener = new ConsumeResponseListener() {
                                    @Override
                                    public void onConsumeResponse(final int responseCode, final String purchaseToken) {
                                        // Log.d("anupam2", responseCode + "  <------->  "+ purchasesResult.getPurchasesList() + "  <------->  " + purchaseToken);
                                    }
                                };
                                mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
                            } else {
    
                            }
                        }
    
                        if (purchasesResult.getPurchasesList().size() > 0) {
                            //  Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");
    
                        } else {
                            //  Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
                            BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
                            int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
                            if (responseCode == 7) {
    
                                //Item already purchased
                            }
                        }
                    }
                }
    
                @Override
                public void onBillingServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                    Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
                }
            });
    
    mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
                @Override
                public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    
                    //  Log.d("anupam", responseCode + "");
                    if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
                        for (Purchase purchase : purchases) {
                        //List of purchases
                        }
                    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
                        Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
                    }
                }
            }).build();
    
    mBillingClient.startConnection(新的BillingClientStateListener(){
    @凌驾
    公共无效onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode){
    if(billingResponseCode==BillingClient.BillingResponse.OK){
    //计费客户端已准备就绪。您可以在此查询购买情况。
    final Purchase.PurchasesResult PurchasesResult=mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
    对于(采购源采购:purchasesResult.getPurchasesList()){
    if(sourcePurchase!=null){
    ConsumerResponseListener侦听器=新ConsumerResponseListener(){
    @凌驾
    ConsumerResponse(最终整数响应代码、最终字符串purchaseToken)上的公共void{
    //Log.d(“anupam2”,responseCode+“”+purchasesResult.getPurchasesList()+“”+purchaseToken);
    }
    };
    mBillingClient.consumerasync(sourcePurchase.getPurchaseToken(),侦听器);
    }否则{
    }
    }
    如果(purchasesResult.getPurchasesList().size()>0){
    //Log.d(“anupam3”,purchasesResult.getPurchasesList().size()+”);
    }否则{
    //Log.d(“anupam4”,purchasesResult.getPurchasesList().size()+”);
    BillingFlowParams.Builder=BillingFlowParams.newBuilder().setSku(“234r23446”).setType(BillingClient.SkuType.SUBS);
    int responseCode=mBillingClient.launchBillingFlow(SplashActivity.this,builder.build());
    如果(响应代码==7){
    //已购买的物品
    }
    }
    }
    }
    @凌驾
    公共无效onBillingServiceDisconnected(){
    //尝试在下一次请求时重新启动连接以
    //Google通过调用startConnection()方法播放。
    Toast.makeText(SplashActivity.this,“失败”,Toast.LENGTH_LONG.show();
    }
    });
    mBillingClient=BillingClient.newBuilder(this.setListener)(新PurchasesUpdatedListener(){
    @凌驾
    公共无效onPurchasesUpdated(int responseCode,@Nullable List purchases){
    //Log.d(“anupam”,responseCode+”);
    if(responseCode==BillingClient.BillingResponse.OK&&purchases!=null){
    用于(采购:采购){
    //采购清单
    }
    }else if(responseCode==BillingClient.BillingResponse.USER\u已取消){
    Toast.makeText(SplashActivity.this,“抱歉,您已取消购买订阅。”,Toast.LENGTH_SHORT.show();
    }否则{
    Toast.makeText(SplashActivity.this,“抱歉,发生了一些错误。”,Toast.LENGTH_SHORT.show();
    }
    }
    }).build();
    
  • 现在,您可以在splashscreen中调用它,并根据响应代码在应用程序中使用SharedReference或全局变量保存一个值。检查订阅id的响应代码是否为7。如果suscribed(对于responseCode==7,您将值保存为suscribed),则显示高级功能,否则不显示


    希望有帮助

    我认为你应该使用queryPurchases,它使用Google Play缓存而不启动网络请求。因此,您将能够知道谁购买了哪种产品,然后进行有限的访问。