在App购买Android没有任何事情发生

时间:2016-02-06 23:17:42

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

关于应用内购买的this指南。不确定我做错了什么,但任何帮助都会很棒!非常棘手,因为它不是通常的活动。只有日志中的东西是ViewRoot的触摸事件:ACTION_UP然后是ACTION_DOWN。

public class Fragment_1 extends Fragment {

IabHelper mHelper;
private Button clickButton;
private Button buyButton;
private static final String TAG =
        "com.appal.inappbilling";
static final String ITEM_SKU = "android.test.purchased";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1,
            container, false);


    buyButton = (Button) view.findViewById(R.id.buyButton);
    buyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonClicked(v);
        }
    });

    clickButton = (Button) view.findViewById(R.id.clickButton);
    clickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v2) {
            buyClick(v2);
        }
    });
    clickButton.setEnabled(false);

    String base64EncodedPublicKey =
            "MYKEY";

    mHelper = new IabHelper(getActivity(), 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");
                                       }
                                   }
                               });

    return view;

}
public void buttonClicked (View v)
{
    clickButton.setEnabled(false);
    buyButton.setEnabled(true);
}
public void buyClick(View v2) {
    mHelper.launchPurchaseFlow(getActivity(), ITEM_SKU, 10001,
            mPurchaseFinishedListener, "mypurchasetoken");
}
@Override
public 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();
            buyButton.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
        } else {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                    mConsumeFinishedListener);
        }
    }
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,
                                          IabResult result) {

                if (result.isSuccess()) {
                    clickButton.setEnabled(true);
                } else {
                    // handle error
                }
            }
        };

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


}

1 个答案:

答案 0 :(得分:1)

您缺少片段中的IabBroadcastReceiver.IabBroadcastListener实现。

当前

public class Fragment_1 extends Fragment { 
  .....
}

像这样修改

public class Fragment_1 extends Fragment implements 

    IabBroadcastReceiver.IabBroadcastListener{ 
          .....


          .....
          @Override
        public void receivedBroadcast() {
            android.util.Log.d(TAG, "Received broadcast notification. Querying inventory.");
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }

        }