使用SharedPreferences禁用应用内购买广告

时间:2016-07-16 08:00:21

标签: android in-app-purchase admob sharedpreferences

我正在尝试通过应用内购买在我的应用中删除AdMob广告。我已经有了隐藏和禁用广告的代码。我还实施了应用内结算。我只需要找到一种方法来保护广告隐藏和禁用,如果用户已经购买。

我在SharedPreferences中存储了一个布尔值,在成功进行应用内购买后,应该永久删除应用中的广告。 此作品,但当应用关闭并重新开启时,广告又会恢复,您无法再次执行购买。

onCreate()方法中:

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {

            } else {

            }
        }
    });

    prefs = this.getSharedPreferences("com.wsandhu.conjugation", Context.MODE_PRIVATE);

    if (adFree) {
        prefs.edit().putBoolean("adFree", true).commit();
    } else {
        adFree = prefs.getBoolean("adFree", false);
    }

应用内结算实施:

/* FOR IN-APP BILLING */
@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)) {

                    // Sets purchased boolean to true
                    adFree = true;

                    // Restart app
                    Intent i = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage( getBaseContext().getPackageName() );
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            }
        };

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
        = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
                                         Inventory inventory) {

        if (result.isFailure()) {
            // Handle failure
        } else {
            Purchase purchase = inventory.getPurchase(ITEM_SKU);
            if (purchase != null) {

            } else {

            }
        }
    }
};

public void buyFullVersion() {
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
            mPurchaseFinishedListener, "mypurchasetoken");
}

我真的不知道自己做错了什么......我已经将应用程序上传到Google Play alpha测试十多次了,而且我有点沮丧每次我等两个小时,它都不起作用。 :P帮助?

1 个答案:

答案 0 :(得分:3)

下面:

adFree = true;

在关闭 adFree变量值并重置为默认值后,用户再次打开应用程序时出现问题。

通过在购买成功时更新SharedPreferences中的状态来执行此操作:

  ...
 }else if (purchase.getSku().equals(ITEM_SKU)) {
     //save value in SharedPreferences here
       prefs.edit().putBoolean("adFree", true).commit();
  }

并在onCreate方法中检查adFree为:

prefs = this.getSharedPreferences("com.wsandhu.conjugation",Context.MODE_PRIVATE);

if(prefs.contains("adFree")){
  if(prefs.getBoolean("adFree", false)){
     // disable ads here
   }else{
      // enable ads here
    }
}else{
  // enable ads
}