谷歌应用程序计费示例(琐碎的驱动器)的onActivityResult方法中的错误?

时间:2014-10-10 08:07:57

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

Google的应用结算示例com.example.android.trivialdrivesample.MainActivity包含以下代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
    if (mHelper == null) return;

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

这似乎有些危险,因为在mHelper初始化之后,onActivityResult才会按预期运行。放弃if (mHelper == null) return支持if (mHelper == null || !mHelper.handleActivityResult(requestCode, resultCode, data))似乎更好,即

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

原始代码真的很危险吗?还是我误解了?

2 个答案:

答案 0 :(得分:1)

嘿我从10天开始就开始从事InApp购买工作,我已成功整合到现有的应用程序中,并准备好实现它。最初,当我开始这样做时,我已经从here下载了名为“Trivial Drive”的谷歌InApp结算示例。

但它对我没什么帮助,因为它有很多问题和错误,所以我决定自己从头开始使用新的v3 api,你可以找到here。本教程有明确的解释,可以帮助您,如果您有时间,请参阅this YouTube视频,Google员工已明确说明如何集成它。

此外,如果您想要快速示例,我还有一个示例应用,可以从here下载。

以下视频还介绍了如何集成InApp Purchase。请仔细阅读。

https://www.youtube.com/watch?v=-h2ESH71hAI

答案 1 :(得分:0)

我完全同意。我认为做这样的事情要好得多:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == REQUEST_CODE_RECOVER_PLAY_SERVICES) {
            if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Google Play Services must be installed.", Toast.LENGTH_SHORT).show();
                finish();
            }
        } else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            // LOAD FROM CAMERA
            ivf.LoadImage(tmpFilePath, quality, resolution, format);

        } else if (requestCode == BUY_PREMIUM && resultCode == Activity.RESULT_OK) {

            this.onPremiumPurchase();

        } else if( requestCode == RC_REQUEST) {

            if (mHelper == null) {
                Toast.makeText(mContext, "mHelper is NULL", Toast.LENGTH_LONG).show();
                return;
            }
            // Pass on the activity result to the helper for handling
            mHelper.handleActivityResult(requestCode, resultCode, data);
        }

    }