重置错误 android.test.purchased

Error Resetting android.test.purchased

我正在开发我的第一个 android 应用程序,它具有一些 in-app 计费功能。在过去的 3 天里,我一直在研究如何在购买我的开发者许可证后添加这些 in-app 购买。我偶然发现了引导我在测试程序中取得成功的指南:

http://www.techotopia.com/index.php/An_Android_Studio_Google_Play_In-app_Billing_Tutorial

测试程序运行良好。但是当我将本指南应用于我的 NavigationDrawerFragment 程序时,我 运行 遇到了一些问题。正如我在标题中所说,我使用 android.test.purchased 作为 SKU,它似乎只让我偶尔使用测试购买。

其他时候我得到"In-app billing error: Unable to buy item, Error response: 7:Item Already Owned"

我在清单文件中添加了计费权限,我也对 Security.java 文件进行了此更改(按照指南):

public static boolean verifyPurchase(String base64PublicKey,
                                     String signedData, String signature) {
    if (TextUtils.isEmpty(signedData) ||
            TextUtils.isEmpty(base64PublicKey) ||
            TextUtils.isEmpty(signature)) {
        Log.e(TAG, "Purchase verification failed: missing data.");
        if (BuildConfig.DEBUG) {
            return true;
        }
        return false;
    }

MainActivity.java - OnCreate - Log.d 代码是//故意的

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

    String base64EncodedPublicKey =
            "My key that i almost pasted in here :P";

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}   

其余 MainActivity.java 与 drawerFragment 无关:

private static final String TAG = "com.example.inappbilling";
static final String ITEM_SKU = "android.test.purchased";

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


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);
                     Context.MODE_PRIVATE);

                    //Never had this code actually run before, could contain errors
                    //but those aren't the issue to my knowledge.
                    SharedPreferences pref = MainActivity.this.getSharedPreferences("MyPref",
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putBoolean("BoughtTabard",true);
                    editor.commit();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.container, TabardGuideNeedToBuy.newInstance(8))
                            .commit();

                } else {
                    // handle error
                }
            }
        };


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

不确定我是否只是忽略了一个小问题或其他问题,但非常感谢任何ideas/solutions!

我确实已经将 IInAppBillingService.aidl 添加到正确的位置,连同所有 9 个 util 文件(Base64、Secruity、SkuDeatils 等...)

如果由于某种原因 android.test.purchased 没有按预期消耗,请添加 consumeItem();在 if (result.isFailure()), 运行 中调用应用程序,重新启动购买流程,然后它会崩溃并消耗 android.test.purchased,然后返回代码并删除 consumeItem ();从 if (result.isFailure()) 中调用。再次重新运行 应用程序,android.test.purchased 现在应该可以再次使用了。