我一直在为此奋斗一个多星期。我正在研究Android上的订阅使用他们的应用内结算版本3我构建了代码并让它运行购买但是因为它去年完成了这个版本不支持订阅,因为我从示例中把它放在一起我不喜欢没有代码来构建使用订阅的代码。
以下是我购买的代码,据我所知,我需要更改这些代码以使用订阅而非购买,并添加代码以确定订阅在Google服务器上是否有效。我现在没有看到代码。
String base64EncodedPublicKey = "MI...QAB";
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set
// this to false).
mHelper.enableDebugLogging(false);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// Oh noes, there was a problem.
// complain("1 "+"Problem setting up in-app billing: " +
// result);
return;
}
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
// ///Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
// complain("2 "+"Failed to query inventory: " + result);
return;
}
// ///Log.d(TAG, "Query inventory was successful.");
// place code here to proceess purchase without reaccessing database
if (inventory.hasPurchase(appSKU)) {
// ///Log.i("Purchase","purchase consumed here");
//mHelper.consumeAsync(inventory.getPurchase(appSKU),mConsumeFinishedListener);
return;
}
}
};
public void order(String appSKU) {
mHelper.launchPurchaseFlow(this, appSKU, RC_REQUEST,
mPurchaseFinishedListener);
}
@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.");
}
}
// 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);
if(purchase == null) {
;
}else {
String tester1 = purchase.toString();
// ///Log.d(TAG, "Purchase successful.");
// mHelper.consumeAsync(purchase, mConsumeFinishedListener);
// myCallServer(udid, calltype, data, OrderID, ProductId, PurchaseToken) {
Bundle myBundle = new Bundle();
myBundle.putString("AppTitle", AppTitle);
Intent myIntent = new Intent(getBaseContext(),
IntroALevActivity.class);
myIntent.putExtras(myBundle);
startActivity(myIntent);
}
}
};
// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
// ///Log.d(TAG, "Consumption finished. Purchase: " + purchase +
// ", result: " + result);
// We know this is the "gas" sku because it's the only one we
// consume,
// so we don't check which sku was consumed. If you have more than
// one
// sku, you probably should check...
if (result.isSuccess()) {
// successfully consumed, so we apply the effects of the item in
// our
// game world's logic, which in our case means filling the gas
// tank a bit
alert("You made a purchase");
} else {
;
}
}
};
我已经在线查看,但我只发现了破碎的代码片段,以帮助我确定检查现有订阅的确切方法代码。如果有人可以查看我的代码并提供我想念的方法,那就太好了。谷歌的例子通常很好,但自从2月15日刚刚订阅了订阅以来,没有很多编码示例需要构建。
我很挣扎,所以如果您有任何问题或需要其他信息,请告诉我,我会尽力提供。
感谢stackoverflow成员
答案 0 :(得分:1)
即使我在我的实现中没有使用订阅,您也可以在IabHelper.QueryInventoryFinishedListener mGotInventoryListener函数中检查它。所以不要这样:
// ///Log.d(TAG, "Query inventory was successful.");
// place code here to proceess purchase without reaccessing database
if (inventory.hasPurchase(appSKU)) {
// ///Log.i("Purchase","purchase consumed here");
//mHelper.consumeAsync(inventory.getPurchase(appSKU),mConsumeFinishedListener);
return;
}
使用此:
Purchase haspurchase = inventory.getPurchase(appSKU);
if (haspurchase = null) {
// do something since there wasn't a purchase
}
else {
// do something if you have the purchase
}
答案 1 :(得分:1)
要使用应用订阅,我想你会在你创建的IabHelper实例上调用“launchSubscriptionPurchaseFlow()”方法。
mHelper.launchSubscriptionPurchaseFlow(Activity act, String sku, int requestCode,
OnIabPurchaseFinishedListener listener, String extraData);
OR
mHelper.launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
OnIabPurchaseFinishedListener listener, String extraData);
其中itemType = IabHelper.ITEM_TYPE_SUBS