重置android.test.purchased时出错

时间:2015-01-08 07:35:59

标签: android in-app-billing

我正在开发我的第一款具有一些应用内结算功能的Android应用。我花了最近3天的时间研究如何在购买开发人员许可后添加这些应用内购买。我偶然发现了一个导致我在测试程序上取得成功的指南:

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

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

其他时候我收到“应用内结算错误:无法购买商品,错误回复:7:商品已经拥有”

我已在清单文件中添加了计费权限,我还对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;
}

不确定我是否只是忽略了一个小问题,但我们非常感谢任何想法/解决方案!

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

1 个答案:

答案 0 :(得分:0)

如果出于某种原因,android.test.purchased在它应该被消耗的时候没有消耗,请添加consumeItem();从if(result.isFailure())调用,运行应用程序,重新启动购买流程,然后崩溃并使用android.test.purchased,然后返回代码并删除co​​nsumeItem();从if(result.isFailure())内部调用。再次重新运行应用程序,现在应该可以再次使用android.test.purchased。