在应用结算中没有用户

时间:2017-07-21 18:35:53

标签: android api google-play-services in-app-billing

我希望在App Billing学习。 我创建了一个测试应用程序。我可以通过getSkuDetails看到我的应用内产品插入Play Console,但是当我启动launchPurchaseFlow时,在应用产品中购买的界面无法启动,我只能看到日志(记录好了)

public class MainActivity extends Activity implements View.OnClickListener {

    Button compra,click,controllo;
    private IabHelper.QueryInventoryFinishedListener mQueryFinishedListener;

    IInAppBillingService mService;

    ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name,
                                       IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
        }
    };

    private static final String TAG="INappBILLING";
    IabHelper mHelper;


    static final String ITEM_SKU="dsada";
    public monky.myapplication.util.IabHelper.OnIabPurchaseFinishedListener 
    mPurchaseFinishedListener;


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

        Intent serviceIntent =
                new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);

        compra=(Button) findViewById(R.id.buybtn);
        compra.setOnClickListener(this);
        click=(Button)findViewById(R.id.clickbtn);
        click.setOnClickListener(this);
        controllo=(Button) findViewById(R.id.controllo);
        controllo.setOnClickListener(this);



        String base64EncodedPublicKey="mykey";

        mHelper=new IabHelper(this,base64EncodedPublicKey);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    // Oh no, there was a problem.
                    Log.d(TAG, "Problem setting up In-app Billing: " + result);
                }
                // Hooray, IAB is fully set up!
            }
        });



        }





    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mService != null) {
            unbindService(mServiceConn);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.clickbtn:

                List additionalSkuList = new ArrayList();
                additionalSkuList.add(ITEM_SKU);
                mHelper.queryInventoryAsync(true, additionalSkuList,
                        mQueryFinishedListener);


                mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
                    public void onQueryInventoryFinished(IabResult result, Inventory inventory)
                    {
                        if (result.isFailure()) {
                            Log.i(TAG, " - Prezzo  Error" );
                            return;
                        }


                        String prezzo = inventory.getSkuDetails(ITEM_SKU).getPrice();

                        String titolo = inventory.getSkuDetails(ITEM_SKU).getTitle();

                        String desc = inventory.getSkuDetails(ITEM_SKU).getDescription();

                        String type = inventory.getSkuDetails(ITEM_SKU).getType();

                        String SkU = inventory.getSkuDetails(ITEM_SKU).getSku();

                        Toast.makeText(getApplicationContext(),"Titolo: "+titolo+" Desc "+desc+" Prezzo "+prezzo+" Tipo "+type+"SKU"+SkU  ,Toast.LENGTH_LONG).show();
                        Log.i(TAG, " - Prezzo " + prezzo);

                        // update the UI
                    }
                };

                break;

            case R.id.buybtn:
                if (!mHelper.isAsyncInProgress()){

                    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
                            mPurchaseFinishedListener,"" );
                    Log.i(TAG, "  sono passato " );}
                else {
                    Log.i(TAG, " Non sono passato " );
                }



                break;


            case R.id.controllo:
                controlloaq();
                break;

        }
    }

    private void controlloaq() {

        IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
                = new IabHelper.OnIabPurchaseFinishedListener() {
            public void onIabPurchaseFinished(IabResult result, Purchase purchase)
            {
                if (purchase.getSku().equals(ITEM_SKU)) {
                    Log.i(TAG, "   passato  e comprato" );
                }

                if (result.isFailure()) {
                    Log.i(TAG, "Error purchasing: " + result);
                    return;
                }



            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
                + data);

        // Pass on the activity result to the helper for handling
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        } else {
            Log.d(TAG, "onActivityResult handled by IABUtil.");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的代码几乎是正确的。发生了什么事情是mPurchaseFinishedListener没有触发,因为你把它放在一个函数controlloaq()内,因此它只会在你调用controlloaq()时执行。

查看官方Google In App Billing sample,除非您知道自己在做什么,否则不应将其包含在函数中。

// Callback for when a purchase is finished
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

            // if we were disposed of in the meantime, quit.
            if (mHelper == null) return;

            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                setWaitScreen(false);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                setWaitScreen(false);
                return;
            }

            Log.d(TAG, "Purchase successful.");

            if (purchase.getSku().equals(SKU_GAS)) {
                // bought 1/4 tank of gas. So consume it.
                Log.d(TAG, "Purchase is gas. Starting gas consumption.");
                try {
                    mHelper.consumeAsync(purchase, mConsumeFinishedListener);
                } catch (IabAsyncInProgressException e) {
                    complain("Error consuming gas. Another async operation in progress.");
                    setWaitScreen(false);
                    return;
                }
            }
            else if (purchase.getSku().equals(SKU_PREMIUM)) {
                // bought the premium upgrade!
                Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
                alert("Thank you for upgrading to premium!");
                mIsPremium = true;
                updateUi();
                setWaitScreen(false);
            }
            else if (purchase.getSku().equals(SKU_INFINITE_GAS_MONTHLY)
                    || purchase.getSku().equals(SKU_INFINITE_GAS_YEARLY)) {
                // bought the infinite gas subscription
                Log.d(TAG, "Infinite gas subscription purchased.");
                alert("Thank you for subscribing to infinite gas!");
                mSubscribedToInfiniteGas = true;
                mAutoRenewEnabled = purchase.isAutoRenewing();
                mInfiniteGasSku = purchase.getSku();
                mTank = TANK_MAX;
                updateUi();
                setWaitScreen(false);
            }
        }
    };