0

我写了一个应用内购买测试应用程序来学习如何在我制作的应用程序中实现它。我改编了谷歌提供的 TrivialDrive 示例中的代码。但它不起作用,在我朋友付款后应用程序崩溃了。代码看起来像这样

public class MainActivity extends Activity {



    String TAG = "AppPurchaseTest";
    IabHelper mHelper;
    boolean mIsPremium = false;
    static final String SKU_PREMIUM = "premium";
    static final int RC_REQUEST = 10001;


 // User clicked the "Upgrade to Premium" button.
    public void onUpgradeAppButtonClicked(View arg0) {
        Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade.");
   //     setWaitScreen(true);
        mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener);
    }


    //this is not working

 // 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);

            int duration = Toast.LENGTH_SHORT;
            if (result.isFailure()) {
                // Oh noes!
             //   complain("Error purchasing: " + result);
             //   setWaitScreen(false);
                Toast.makeText(getBaseContext(), "Fail :(", duration).show();
                return;
            }

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

            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;

                Toast.makeText(getBaseContext(), "Successo: adesso sei premium", duration).show();
                Button test = (Button) findViewById(R.id.test);
                test.setVisibility(View.INVISIBLE);
              //  updateUi();
             //   setWaitScreen(false);
            }
        }
    };


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

        String base64EncodedPublicKey = null;

        // compute your public key and store it in base64EncodedPublicKey
        mHelper = new IabHelper(this, base64EncodedPublicKey);

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


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        if (mHelper != null) {
            Log.d(TAG, "mHelper doesn't = null ");
        mHelper.dispose();
        mHelper = null;
        }
    }

    @Override
    protected 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.");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

你能看出哪里不对吗?我忘记了什么?

本教程https://developer.android.com/google/play/billing/billing_integrate.html 看起来更简单,但我不明白如何实现它,是否有示例或其他东西可以让我看到它是如何实现的实施的?我只需要简单升级到高级购买

很难让它工作,因为我不能亲自测试它,每次我测试它我都会赔钱:(

4

2 回答 2

1

花了很长时间才弄清楚我的项目,但您必须了解您的 mPurchaseFinishedListener 是在非渲染线程上调用的,并且在您的应用程序 onResume() 被调用之前(只需检查 IabHelper 代码。

因此,如果您尝试在那里进行任何渲染,它可能会崩溃,因为渲染上下文尚未恢复。

在您的情况下,很可能是:

Toast.makeText(getBaseContext(), "Successo: adesso sei premium", duration).show();

会崩溃,同样适用

Button test = (Button) findViewById(R.id.test);

如果您检查琐事示例,则 textButton 可见性设置为 true,但按钮本身是在 onCreate() 方法中分配的类属性。

让我知道这是否有帮助..

于 2014-10-17T08:18:45.927 回答
0

Per TrivialDrive 您还需要(这将解决它):

 // We're being destroyed. It's important to dispose of the helper here!
    @Override
    public void onDestroy() {
        super.onDestroy();

        // very important:
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }

        // very important:
        Log.d(TAG, "Destroying helper.");
        if (mHelper != null) {
            mHelper.disposeWhenFinished();
            mHelper = null;
        }
    }
于 2018-12-18T03:46:46.313 回答