为什么我的应用内结算移除广告功能无效?

时间:2015-08-09 15:45:51

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

我正在尝试创建一个在用户进行购买时停用广告的应用。但是,广告甚至不会显示应用程序当前启动的时间。

如果我将其放在onCreate方法中,则广告只会在用户启动应用时加载。 (当用户点击删除广告按钮时,广告就会消失。)

mAdView = (AdView) findViewById(R.id.adView); adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest);

这是我的MainActivity.class

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
IabHelper mHelper;
AdView mAdView;
MenuItem menuItem2;
AdRequest adRequest;
NavigationView nvDrawer;
DrawerLayout mDrawer;
RelativeLayout myView;
RecyclerView recyclerView;
AppBarLayout secondview;
SharedPreferences prefs;
SharedPreferences.Editor editor;
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener;
CoordinatorLayout container;

private ActionBarDrawerToggle drawerToggle;
public static final String TAG =
        "iboche.time.google.com.ibche.inappbilling";
static final String ITEM_SKU = "remove_ads";
final String mIsPremium = "purchased";

    @SuppressWarnings("ConstantConditions")
@Override
protected void onCreate(Bundle savedInstanceState) {
    String base64EncodedPublicKey =
            "MY PUBLIC KEY";

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    menuItem2 = (MenuItem) findViewById(R.id.nav_second_fragment);
    nvDrawer = (NavigationView) findViewById(R.id.nvView);
    adscontainer = (RelativeLayout) findViewById(R.id.container2);
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    queryPurchasedItems();

    if(prefs.getBoolean(mIsPremium, false)){
    Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    }
    else if(prefs.getBoolean(mIsPremium,true)) {
    mAdView.setVisibility(View.GONE);
    Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
    }
    setupDrawerContent(nvDrawer);
    drawerToggle = setupDrawerToggle();
    mDrawer.setDrawerListener(drawerToggle);
    mHelper.startSetup(new
                               IabHelper.OnIabSetupFinishedListener() {
                                   public void onIabSetupFinished(IabResult result) {
                                       if (!result.isSuccess()) {

                                           mHelper.queryInventoryAsync(mReceivedInventoryListener);
                                           Log.d(TAG, "Failed:");
                                       } else if (result.isSuccess()){

                                           Log.d(TAG, "OK");
                                       }
                                   }
                               });

}

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

        if (result.isFailure()) {
            // Handle failure


        }
        else {


            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                    mConsumeFinishedListener);
                              editor = prefs.edit();
                    editor.putString("purchased", mIsPremium);
                    editor.putBoolean(mIsPremium, true);
                    editor.commit();

                    Toast.makeText(MainActivity.this,"premium", Toast.LENGTH_LONG).show();

        }



    }
};
public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);

}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Sync the toggle state after onRestoreInstanceState has occurred.
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    drawerToggle.onConfigurationChanged(newConfig);
}
private ActionBarDrawerToggle setupDrawerToggle() {
    return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
}
private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    selectDrawerItem(menuItem);
                    selectDrawerItem1(menuItem);
                    return true;
                }
            });}

public void selectDrawerItem(MenuItem menuItem) {

    mHelper.launchPurchaseFlow(this, ITEM_SKU, 0,
            mPurchaseFinishedListener, "GENERIC TOKEN");

    mDrawer.closeDrawers();

    //if (mIsPremium){

    menuItem.setTitle("Purchased");
    mAdView.setVisibility(View.GONE);
}
public void selectDrawerItem1(MenuItem menuItem) {

    mDrawer.closeDrawers();
}






@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()) {

            if (result.getResponse() == IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {



                return;
            }
            // Handle error

        }
        else if(purchase.getSku().equals(ITEM_SKU)) {

            editor = prefs.edit();
            editor.putString("purchased", mIsPremium);
            editor.putBoolean(mIsPremium, true);
            editor.commit();
            consumeItem();
        }
    }


};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,
                                          IabResult result) {

                if (result.isSuccess()) {


                } else {
                    // handle error

                }
            }
        };
    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawer.openDrawer(GravityCompat.START);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();

    mHelper = null;

        }

}

1 个答案:

答案 0 :(得分:0)

我要做的是为iap事件的共享pref删除添加,然后在用户执行iap时更改它....然后在onCreate上检查它是否禁用广告并相应地设置{ {1}}根据共享的首选项在布局中添加容器。

尝试只调用一次SharedPreference,然后使用编辑器提交数据:

Visibilty = "GONE"

这是针对onCreate

            SharedPreference SP;
            SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            editor = SP.edit();

            //...
                editor.putString("abc", udata);
                editor.commit();
            //...
                editor.putStringSet("si",ssetdata)
                editor.commit();

详细了解共享pref here