1

尝试在我的应用程序中设置应用程序购买,它位于一个片段中。让它在一个活动中工作,但努力把它变成一个片段。如果有人可以看到任何错误或可以帮助我,那就太好了。它只是进入没有显示应用内购买的活动。

代码:

public class Fragment_11 extends Fragment implements OnClickListener {

private static final String TAG = "BillingService";

private Context mContext;

boolean mIsRegistered = false;

// this has already been set up for my app at the publisher's console
static final String IS_REGISTERED = "myregistered";

static final int RC_REQUEST = 10001;

 // The helper object
IabHelper mHelper; 


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

String base64EncodedPublicKey = "[my public key]"; // (from publisher's console for my app)

// Create the helper, passing it our context and the public key to verify signatures with
Log.d(TAG, "Creating IAB helper.");
mHelper = new IabHelper(getActivity(), base64EncodedPublicKey);

// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(true);

// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
    public void onIabSetupFinished(IabResult result) {
        Log.d(TAG, "Setup finished.");

        if (!result.isSuccess()) {
            complain("Problem setting up in-app billing: " + result);
            return;
        }

        // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
        Log.d(TAG, "Setup successful. Querying inventory.");
        mHelper.queryInventoryAsync(mGotInventoryListener);
    }
});

   // Set the onClick listeners
  view.findViewById(R.id.button1).setOnClickListener(this);

 Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getActivity(), player11.class);
        getActivity().startActivity(intent);
    }
});
  Button button2 = (Button) view.findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getActivity(), player12.class);
            getActivity().startActivity(intent);
        }
    });
      Button button3 = (Button) view.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getActivity(), player13.class);
                getActivity().startActivity(intent);
            }
        });
          Button button4 = (Button) view.findViewById(R.id.button4);
            button4.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(getActivity(), player14.class);
                    getActivity().startActivity(intent);
                }
            });
              Button button5 = (Button) view.findViewById(R.id.button5);
                button5.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(getActivity(), player15.class);
                        getActivity().startActivity(intent);
                    }
                });

            return view;
            }

            // Listener that's called when we finish querying the items we own
            IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
                public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                    Log.d(TAG, "Query inventory finished.");
                    if (result.isFailure()) {
                        complain("Failed to query inventory: " + result);
                        return;
                    }

                    Log.d(TAG, "Query inventory was successful.");

                    // Do we have the premium upgrade?
                    mIsRegistered = inventory.hasPurchase(IS_REGISTERED);
                    Log.d(TAG, "User is " + (mIsRegistered ? "REGISTERED" : "NOT REGISTERED"));

                    setWaitScreen(false);
                    Log.d(TAG, "Initial inventory query finished; enabling main UI.");
                }
            };      

            // User clicked the "Register" button.
            private void startRegistered() {
                Log.d(TAG, "Register button clicked; launching purchase flow for upgrade.");
                setWaitScreen(true);
                mHelper.launchPurchaseFlow(getActivity(), IS_REGISTERED, RC_REQUEST, mPurchaseFinishedListener);
            }

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

                // Pass on the activity result to the helper for handling
                if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
                    // not handled, so handle it ourselves (here's where you'd
                    // perform any handling of activity results not related to in-app billing..
                    super.onActivityResult(requestCode, resultCode, data);
                }
                else {
                    Log.d(TAG, "onActivityResult handled by IABUtil.");
                }
            }

            // 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 (result.isFailure()) {
                        // Oh noes!
                        complain("Error purchasing: " + result);
                        setWaitScreen(false);
                        return;
                    }

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

                    if (purchase.getSku().equals(IS_REGISTERED)) {
                        Log.d(TAG, "User has registered..");
                        alert("Thank you.");
                        mIsRegistered = true;
                        setWaitScreen(false);
                    }
                }
            };

            // We're being destroyed. It's important to dispose of the helper here!
            @Override
            public void onDestroy() {
                // very important:
                Log.d(TAG, "Destroying helper.");
                if (mHelper != null) mHelper.dispose();
                mHelper = null;
            }

            void complain(String message) {
                Log.e(TAG, "**** Register Error: " + message);
                alert("Error: " + message);
            }

            void setWaitScreen(boolean set) {
                // just a dummy for now
            }

            void alert(String message) {
                AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
                bld.setMessage(message);
                bld.setNeutralButton("OK", null);
                Log.d(TAG, "Showing alert dialog: " + message);
                bld.create().show();
            }
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.button1:
                    startRegistered();
                    break;
                default:
                    break;
                }


  }}
4

2 回答 2

1

可能问题出在 onActivityResult 中。我有问题。您需要像在片段中一样在活动中覆盖它。如果您不覆盖 onActivityResult,它将启动它的超类,并且您的片段将不会启动 OnIabPurchaseFinishedListener。

于 2014-05-22T08:02:42.950 回答
0

我有这个问题。我所做的是在主要活动中实施购买。所以我将哪个 SKU 发送到主要活动并在那里进行处理。购买完成后,我设置了一个标志天气,它成功与否。然后重新启动片段并检查 onresume 函数中的标志。

于 2016-06-23T06:28:54.703 回答