Android在App Billing中进行多项活动沟通

时间:2016-09-30 18:46:51

标签: android in-app-purchase in-app-billing

我的应用程序中有2个活动:第一个是主菜单,另一个是用户可以购买高级升级的活动。

主菜单:

    String TAG = "pay";
    IabHelper mHelper;
    static final String ITEM_SKU = "android.test.purchased";
    static boolean hasPremium;

    boolean isBound = false;
    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);

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

            UpdatePremium( );

        }
    };



    @Override
    protected void onStart() {
        super.onStart();

        String base64EncodedPublicKey = "vbcxgfdsfd";

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
               public void onIabSetupFinished(IabResult result)
               {
                   if (!result.isSuccess()) {
                       Log.d(TAG, "In-app Billing setup failed: " +
                                          result);
                   } else {
                       Log.d(TAG, "In-app Billing is set up OK");
                   }
               }
           });
    }


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

        Log.d("main", "oncreate");


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


    purchaseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


                try {
                    PurchasePremiumUpgrade( );
                } catch (IabHelper.IabAsyncInProgressException e) {
                    e.printStackTrace( );
                }


                UpdatePremium( );

            }

        });
    }
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
            = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result,
                                          Purchase purchase) throws IabHelper.IabAsyncInProgressException {

            Log.d(TAG, "OnIabPurchaseFinishedListener");
            if (result.isFailure()) {
                Log.d(TAG, "isFailure()");
                return;
            }
            else if (purchase.getSku().equals(ITEM_SKU)) {
                Log.d(TAG, "purchased!");
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }

        }
    };

    IabHelper.QueryInventoryFinishedListener mGotInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {

            Log.d(TAG, "QueryInventoryFinishedListener");

            if (result.isFailure()) {
                // handle error here
                Log.d(TAG, "is Failure");
            }
            else {
                // does the user have the premium upgrade?
                hasPremium = inventory.hasPurchase(ITEM_SKU);
            }
        }
    };

    public void PurchasePremiumUpgrade() throws IabHelper.IabAsyncInProgressException {
        mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
                mPurchaseFinishedListener, "premiumupgrade");
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        try {
            if (!mHelper.handleActivityResult(requestCode,
                    resultCode, intent)) {
                super.onActivityResult(requestCode, resultCode, intent);
            }
        } catch (IabHelper.IabAsyncInProgressException e) {
            e.printStackTrace( );
        }


        if (requestCode == 10001) {
            int responseCode = intent.getIntExtra("RESPONSE_CODE", 0);
            String purchaseData = intent.getStringExtra("INAPP_PURCHASE_DATA");
            String dataSignature = intent.getStringExtra("INAPP_DATA_SIGNATURE");

            if (resultCode == RESULT_OK) {
                try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String purchaseToken = jo.optString("token", jo.optString("purchaseToken"));
                    Log.d("purchaseToken", purchaseToken);

                    String sku = jo.getString("productId");
                    Toast.makeText(this, "You have bought the " + sku + ". Excellent choice,\n" +
                                                 "adventurer!\"", Toast.LENGTH_SHORT).show( );

                    hasPremium = true;
                }
                catch (JSONException e) {
                    Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show( );
                    e.printStackTrace();
                }
            }
        }

public void UpdatePremium () {

        Bundle ownedItems = null;

        try {
            ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
        } catch (RemoteException e) {
            e.printStackTrace( );
        }

        int response = ownedItems.getInt("RESPONSE_CODE");
        Log.d("response code", String.valueOf(ownedItems.getInt("RESPONSE_CODE")));
        if (response == 0) {
            ArrayList<String> ownedSkus =
                    ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
            ArrayList<String>  purchaseDataList =
                    ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
            ArrayList<String>  signatureList =
                    ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE_LIST");
            String continuationToken =
                    ownedItems.getString("INAPP_CONTINUATION_TOKEN");

            for (int i = 0; i < purchaseDataList.size(); ++i) {
                String purchaseData = purchaseDataList.get(i);
                String signature = signatureList.get(i);
                String sku = ownedSkus.get(i);

                if (sku.equals("android.test.purchased")) {
                    hasPremium = true;
                    return;
                }

                Log.d("purchaseData", purchaseData);
                Log.d("signature", signature);
                Log.d("sku", sku);
            }

            hasPremium = false;

        }

    }

以上是有效的(尽管onQueryInventoryFinished永远不会被调用,而onIabPurchaseFinished result.isFailure()总是如此)

我的问题是,如果我将所有这些代码都放在购买活动中,我如何检查用户是否从主菜单中获得了溢价?该活动本身没有信息(mservice,mServiceConn,mhelper等)。

另外,我怀疑应用程序结算中的代码太多了。如果您认为我应该删除一些部件,请告诉我。我迷失在这里......

0 个答案:

没有答案