简体   繁体   English

为无广告用户安全地从LinearLayout中删除AdView

[英]Safely Removing an AdView from a LinearLayout for Ad Free Users

I have an Android App with a Banner Ad in it. 我有一个带有横幅广告的Android应用。 I only put in the AdView element into the LinearLayout after I have checked ads are meant to be displayed. 仅在检查广告是否可以显示之后,才将AdView元素放入LinearLayout中。

However, the user can purchase NoAds during the run time of the program. 但是,用户可以在程序运行期间购买NoAd。 This means I have to safely stop the ad and remove the Adview from the program without falling foul of Admob rules where you cannot hide adverts. 这意味着我必须安全地停止广告并将Adview从程序中删除,而不会违反Admob规则,在该规则中您无法隐藏广告。

Here is my code, but I am very unsure about the section in the if statement on the line if (mAdView != null) { // It might not be created yet . 这里是我的代码,但我对在部分非常不确定if上线的语句if (mAdView != null) { // It might not be created yet

Is that just hiding it, not removing it completely?: 那只是隐藏它,而不是完全删除它吗?:

public void turnAdvertsOnOff(boolean on) {
    advertsOn = on;

    // IMPORTANT - changes to layout must be done on the UI Thread or will get error
    m_Context.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            LinearLayout myLayout;
            // NEW
            //AdView mAdView;

            myLayout = (LinearLayout) m_Context.findViewById(R.id.myLayout);
            //mAdView = (AdView) m_Context.findViewById(R.id.adView);

            if(advertsOn){
                if(mAdView == null && myLayout != null) {
                    // Create it and add it to the LinearLayout
                    mAdView = new AdView(m_Context);
                    // Sizes BANNER, SMART_BANNER, LARGE_BANNER
                    mAdView.setAdSize(AdSize.SMART_BANNER);
                    mAdView.setAdUnitId(adIdBanner);
                    myLayout.addView(mAdView);
                }
                if (mAdView != null) mAdView.setVisibility(View.VISIBLE);
            }else {
                if (mAdView != null) { // It might not be created yet
                    mAdView.pause();
                    mAdView.setEnabled(false);
                    mAdView.setVisibility(View.GONE);
                    //mAdView.destroy();
                    //mAdView.setVisibility(View.INVISIBLE);
                    //mAdView.pause();
                    //mAdView.setVisibility(View.GONE);
                }
            }
        }});
}

The preferred way is to remove() the AdView from its parent. 首选方法是从其父项中remove() AdView
Why not set Visibility to GONE? 为什么不将“可见性”设置为“消失”?
If you set the visibility to GONE, the AdView will still request the Ad Servers to fill in the Ads, so its better to completely remove the AdView . 如果您将可见性设置为GONE,则AdView仍将请求Ad Server填充广告,因此最好完全删除AdView

For example - 例如 -
Add the AdView in you layout file, if the User has removed the Ads via IAP, just call myLayout.removeView(mAdView); 如果用户已通过IAP删除了广告,则在布局文件中添加AdView,只需调用myLayout.removeView(mAdView);

Also there's no need to call runOnUiThread() if you are calling turnAdvertsOnOff() from the ui thread. 同样,如果您正在从ui线程中调用turnAdvertsOnOff() ,则无需调用runOnUiThread()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM