应用内结算删除广告在付款后但在销毁应用后没有工作

时间:2015-06-24 14:42:16

标签: android in-app-purchase in-app-billing

我第一次尝试使用应用内结算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);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要移动if语句

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

onQueryInventoryFinished。这是因为您在应用有机会检查用户是否已购买升级之前展示广告,然后在检查完成后,您再也不会隐藏广告。

因此,请从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,因此这些确切的代码可能无效,但是在检查购买后您没有隐藏广告的一般想法是正确的。