帮助程序中未调用 OnIabPurchaseFinishedListener class
OnIabPurchaseFinishedListener not called in helper class
目前正在 Android 中尝试在应用程序结算中使用。
一切正常,我可以购买商品并查询现有商品,但我的 OnIabPurchaseFinishedListener 侦听器在成功购买期间未被调用。它在出现问题时调用,但不会在完成购买时调用。
我有我的主 activity,带有片段和一个导航抽屉。 (这里不涉及片段。我有一个助手 class,所有的账单都在其中发生。
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, MyBilling.Update {\
MyBilling bill;
OnCreate(){
bill = new MyBilling(this, this);
bill.onCreate();
}
private void RemoveAdsClick()
{
bill.purchaseRemoveAds();
}
public void NewPurchaseUpdate(){
tinydb.putBoolean("Premium", true);
nav_Menu.findItem(R.id.remove_ad_button).setVisible(false);
displaySelectedScreen(R.id.distance_check); // Reload screen
}
}
public class MyBilling extends Activity {
////Interface
public interface Update {
void NewPurchaseUpdate();
}
// Debug tag, for logging
static final String TAG = "XXXXXX";
static final String SKU_REMOVE_ADS = "remove_ads";
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10111;
// Activity
Activity activity;
// The helper object
IabHelper mHelper;
String base64EncodedPublicKey = "xxxxxxxx";
String payload = "xxxxx";
public boolean isAdsDisabled = false;
public boolean AdCheck = false;
////Instance of interface
Update myActivity;
public MyBilling(Activity launcher,Update activity) {
this.activity = launcher;
myActivity = activity;
}
// User clicked the "Remove Ads" button.
public void purchaseRemoveAds() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
mHelper.launchPurchaseFlow(activity, SKU_REMOVE_ADS,
RC_REQUEST, mPurchaseFinishedListener, payload);
}
});
}
@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.");
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
{
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: "
+ purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null)
return;
if (result.isFailure()) {
complain("Error purchasing: " + result);
return;
}
if (!verifyDeveloperPayload(purchase)) {
complain("Error purchasing. Authenticity verification failed.");
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU_REMOVE_ADS)) {
// bought the premium upgrade!
myActivity.NewPurchaseUpdate();
Log.d(TAG, "New Purchase Update Method was called");
}
}
};
}
一些谷歌搜索表明,这可能是 onActivityResult 不在助手中的问题 class。所以我将 billing class extend activity 然后添加了 onActivityResult 和 @Override 但这也没有用任何断点调用。
如上所述,OnIabPurchaseFinishedListener 会在购买失败(已拥有商品)时调用,但不会在成功购买时调用。
如果有任何帮助,我将不胜感激,我已尝试使代码尽可能简洁以帮助阅读。如果我遗漏了什么,请告诉我。
我暂时放弃了助手 class 并将计费任务移至主要 activity。
从而解决了这个问题
目前正在 Android 中尝试在应用程序结算中使用。
一切正常,我可以购买商品并查询现有商品,但我的 OnIabPurchaseFinishedListener 侦听器在成功购买期间未被调用。它在出现问题时调用,但不会在完成购买时调用。
我有我的主 activity,带有片段和一个导航抽屉。 (这里不涉及片段。我有一个助手 class,所有的账单都在其中发生。
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, MyBilling.Update {\
MyBilling bill;
OnCreate(){
bill = new MyBilling(this, this);
bill.onCreate();
}
private void RemoveAdsClick()
{
bill.purchaseRemoveAds();
}
public void NewPurchaseUpdate(){
tinydb.putBoolean("Premium", true);
nav_Menu.findItem(R.id.remove_ad_button).setVisible(false);
displaySelectedScreen(R.id.distance_check); // Reload screen
}
}
public class MyBilling extends Activity {
////Interface
public interface Update {
void NewPurchaseUpdate();
}
// Debug tag, for logging
static final String TAG = "XXXXXX";
static final String SKU_REMOVE_ADS = "remove_ads";
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10111;
// Activity
Activity activity;
// The helper object
IabHelper mHelper;
String base64EncodedPublicKey = "xxxxxxxx";
String payload = "xxxxx";
public boolean isAdsDisabled = false;
public boolean AdCheck = false;
////Instance of interface
Update myActivity;
public MyBilling(Activity launcher,Update activity) {
this.activity = launcher;
myActivity = activity;
}
// User clicked the "Remove Ads" button.
public void purchaseRemoveAds() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
mHelper.launchPurchaseFlow(activity, SKU_REMOVE_ADS,
RC_REQUEST, mPurchaseFinishedListener, payload);
}
});
}
@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.");
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
{
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: "
+ purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null)
return;
if (result.isFailure()) {
complain("Error purchasing: " + result);
return;
}
if (!verifyDeveloperPayload(purchase)) {
complain("Error purchasing. Authenticity verification failed.");
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU_REMOVE_ADS)) {
// bought the premium upgrade!
myActivity.NewPurchaseUpdate();
Log.d(TAG, "New Purchase Update Method was called");
}
}
};
}
一些谷歌搜索表明,这可能是 onActivityResult 不在助手中的问题 class。所以我将 billing class extend activity 然后添加了 onActivityResult 和 @Override 但这也没有用任何断点调用。
如上所述,OnIabPurchaseFinishedListener 会在购买失败(已拥有商品)时调用,但不会在成功购买时调用。
如果有任何帮助,我将不胜感激,我已尝试使代码尽可能简洁以帮助阅读。如果我遗漏了什么,请告诉我。
我暂时放弃了助手 class 并将计费任务移至主要 activity。
从而解决了这个问题