我已经使用API 3实现了应用程序内购买,并且测试我发布了apk作为alpha测试版本。 我可以使用我的测试帐户购买,它工作正常。但我需要检查恢复功能,但购买状态不会随着apk在alpha测试中而改变。
如何在发布之前检查还原功能?请帮帮我。
mHelper = new IabHelper(this, 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()) {
// Oh noes, there was a problem.
complain("Problem setting up in-app billing: " + result);
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
ArrayList<String> skuList = new ArrayList<String> ();
skuList.add("purchase.test");
skuList.add("purchase.test2");
skuArray = new JSONArray(skuList);
mHelper.queryInventoryAsync(true, skuList, mQueryFinishedListener);
}
});
}
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory)
{
if (result.isFailure()) {
Log.v("Menu", "RESULT FALIURE");
return;
}
Log.v("Menu", "this +" + skuArray);
Log.v("Menu", "Inventory +" + inventory);
for(int i = 0; i < skuArray.length(); i++){
try {
String SKU = skuArray.getString(i);
if(inventory.getSkuDetails(SKU) != null){
Toast.makeText(getApplicationContext(), "SKU = " + SKU+" .... "+inventory.hasPurchase(SKU), Toast.LENGTH_LONG).show();
Log.v("Menu", "SKU = " + SKU+" .... "+inventory.hasPurchase(SKU));
Log.v("Menu", "SKU" + SKU + "= " + inventory.getSkuDetails(SKU).getTitle());
}else{
Log.v("Menu", "SKU RETURNED NULL" + SKU);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
答案 0 :(得分:1)
每次应用程序打开时,您都可以调用getPurchases()方法,如果用户已经购买了该项,此方法将返回您的交易详情。这样您就可以检查恢复功能。请参阅以下link。
Bundle ownedItems = mservice.getPurchases(3, getPackageName(), "inapp",null);
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0) {
//user has purchased the item
//do something here
} else {
//user not purchased
}