Android - 成功购买应用内结算后,按钮消失

时间:2017-06-27 17:51:58

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

我是Android新手,我正在尝试为我的应用实现应用结算。我所拥有的是一个按钮,可以将您带到InAppBillingActivity,而活动就是这样。我想要做的是当购买成功时,带您进入该活动的按钮将消失。我不能让它消失,我可能不会做得对。我必须按下后退按钮才能回到它来自的片段,似乎没有做到正确。所以这是我的代码:

public class InAppBillingActivity extends AppCompatActivity {

    private static final String TAG = "com.android.inappbilling";
    public IabHelper mHelper;
    private Button btnInAppBilling;

    public boolean isPurchased;
    private CheckListGatherFragment checkListGatherFragment;
    static final String ITEM_SKU = "android.test.purchased";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_app_billing);
        btnInAppBilling = (Button) findViewById(R.id.btnInAppBilling);

        checkListGatherFragment = new CheckListGatherFragment();
        String base64EncodedPublicKey = "";

        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");
                }
            }
       });
  }

public void buyClick(View view){
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
            mPurchaseFinishedListener, "mypurchasetoken");
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data)
{
    if (!mHelper.handleActivityResult(requestCode,
            resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,
                                      Purchase purchase)
    {
        if (result.isFailure()) {
            // Handle error
            return;
        }
        else if (purchase.getSku().equals(ITEM_SKU)) {
            consumeItem();
            btnInAppBilling.setEnabled(false);
        }

    }
};

public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

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

        if (result.isFailure()) {
            // Handle failure
            return;
        } else {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                    mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,
                                          IabResult result) {

                if (result.isSuccess()) {

                  //Here is where I am trying to get it to disappear 
                 checkListGatherFragment.btnPurchase.setVisibility(View.GONE);
                 //Tried a boolean to get it to disappear
                    isPurchased = true;
                } else {
                    // handle error
                    return;
                }
            }
        };

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
}


}

以下是我的一些片段:

private InAppBillingActivity activity;
public  Button btnPurchase;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   btnPurchase = (Button) rootView.findViewById(R.id.btnPurchase);
   activity = new InAppBillingActivity();
   setOnClick();
   //tried to do it this way
   if (activity.isPurchased == true){
      btnPurchase.setVisibility(View.GONE);
   }
}

public void setOnClick() {
    btnPurchase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CheckListGatherFragment.this.getActivity(), InAppBillingActivity.class);
            startActivity(intent);
            //tried doing it this way
            if (activity.isPurchased == true){
                btnPurchase.setVisibility(View.GONE);
            }
        }
    });
 } 

//tried doing it this way
@Override
public void onResume() {
    super.onResume();

    if (activity.isPurchased == true){
        btnPurchase.setVisibility(View.GONE);
    }
}

我正在尝试各种不同的方法来尝试让按钮消失,但我无法实现。购买成功,但不会使按钮消失。任何帮助将非常感激。谢谢!

2 个答案:

答案 0 :(得分:0)

当你回到你的片段时,它已经被创建了,所以onCreateView和onResume都不会被调用。

您可以做的是在调用片段中实现onActivityResult,使用startActivityForResult(intent,requestCode),并让InAppBillingActivity返回结果。方法如下:

在您的调用片段中进行以下更改:

public void setOnClick() {
    btnPurchase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CheckListGatherFragment.this.getActivity(), InAppBillingActivity.class);
            startActivityForResult(intent, 0);
        }
    });
 } 

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

    if (resultCode == InAppBillingActivity.RESULT_SUCCESS) {
        btnPurchase.setVisibility(View.GONE);
    } else {
        //handle error here
    }
}

InAppBillingActivity中的这些更改:

public static final int RESULT_SUCCESS = 1;
public static final int RESULT_FAILED = 0;

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,
                                          IabResult result) {

                if (result.isSuccess()) {
                  InAppBillingActivity.this.setResult(RESULT_SUCCESS);
                } else {
                  InAppBillingActivity.this.setResult(RESULT_FAILED);
                }

                InAppBillingActivity.this.finish();
            }
        };

让我知道它是否有效。

答案 1 :(得分:-1)

您在布局中添加片段的位置?