应用程序内计费删除广告在付款后有效,但在销毁应用程序后无效



我第一次尝试使用应用程序内填充API。我用一个不同的账户测试了这个应用程序,付款通过了,广告也从应用程序中删除了。然而,当我销毁了这个应用程序,他们再次启动它时,广告又出现了。我不明白我做错了什么。有人有什么建议吗?

这是我在主要活动中的代码:

public class MainActivity extends AppCompatActivity {
private static final String TAG =  MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private RecyclerView mRecyclerView;
private ArrayList<String> shoppingListItems;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private TextView mEmptyTextView;
private ShoppingListAdapter adapter;
private ActionButton actionButton;
private MaterialDialog addItemdialog = null;
private AdView mAdView;
private IabHelper mHelper;
private String SKU_REMOVE_ADDS = "remove_adds_sku";
private boolean mIsRemoveAdds = false;
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
            // consume the gas and update the UI
            mIsRemoveAdds = true;
            mAdView.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mEmptyTextView = (TextView)findViewById(R.id.list_empty);
    mEmptyTextView.setVisibility(View.INVISIBLE);
    mSharedPreferences = getPreferences(MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();

    queryPurchasedItems();
    //load ads
    if(!mIsRemoveAdds) {
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }else{
        mAdView.setVisibility(View.GONE);
    }
String publicKey = s1+s2+s3+s4+s5;
    mHelper = new IabHelper(this,publicKey);
    if(mHelper != null) {
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            @Override
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    //error
                    Log.d(TAG, "Proglem setting up in-app Billing: " + result);
                }
                //Horay, IAB is fully set up!
                Log.d(TAG, "Horay, IAB is fully set up!");
            }
        });
    }
}
private void queryPurchasedItems() {
    //check if user has bought "remove adds"
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                // handle error here
                Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
            }
            else{
                // does the user have the premium upgrade?
                mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
                if(!mIsRemoveAdds){
                    Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                }
                // update UI accordingly
                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
            }
        }
    };
}
@Override
protected void onResume() {
    super.onResume();
    queryPurchasedItems();
    //Toast.makeText(MainActivity.this,"On Resume",Toast.LENGTH_LONG).show();
    //read isRemoveAdds from shared preferences
    //mIsRemoveAdds = mSharedPreferences.getBoolean(Constants.IS_REMOVE_ADDS,false);
    if(!mIsRemoveAdds) {
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }else{
        mAdView.setVisibility(View.GONE);
    }
    isListEmpty();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
    mAdView.destroy();
}
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
 if(id == R.id.action_remove_adds){
        mHelper.launchPurchaseFlow(this,SKU_REMOVE_ADDS,1,mPurchasedFinishedListener,"");
    }
    return super.onOptionsItemSelected(item);
}
 @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...
        //setContentView(R.layout.activity_main_adds);
        super.onActivityResult(requestCode, resultCode, data);
    }
    else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
        //setContentView(R.layout.activity_main_adds);
    }
}

您需要移动if语句

if(!mIsRemoveAdds) {
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}else{
    mAdView.setVisibility(View.GONE);
}

至CCD_ 1。这是因为你在应用程序有机会检查用户是否购买了升级之前就显示了广告,然后在检查完成后,你再也不会隐藏广告了。

因此,从onCreateonResume方法中删除上面的if语句,并将onQueryInventoryFinished方法格式化为:

    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            // handle error here
            Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
        }
        else{
            // does the user have the premium upgrade?
            mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
            if(!mIsRemoveAdds){
                Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                // user does not have premium, load ads
                mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            } else {
                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
                // user has premium, hide ads
                mAdView.setVisibility(View.GONE);
            }
        }
    }

免责声明我以前从未使用过购买api,所以这个确切的代码可能不起作用,但你在检查购买后没有隐藏广告的总体想法是正确的。

最新更新