如何避免在加载广告横幅时给用户带来不便?

How to avoid inconvenience for users when loading an ad banner?

我不想在第一次显示广告横幅,只需在第 3、6、9 次加载广告即可。我能怎么做 ? 如果我想让广告横幅在 1 分钟后自动关闭,我该怎么做? 如果用户想关闭广告,有什么办法吗?

这是在关注@Nana Ghartey:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);

loadTimedAd();
autoCloseAd() ;
}


int adCounter;    
private void loadTimedAd() {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        adCounter = sharedPrefs.getInt("prefAdCounter", 0);
if (adCounter % 2 == 0 && connectedToNet())
adshow();
    //            adView.loadAd(new AdRequest.Builder().build());

else PreferenceManager.getDefaultSharedPreferences(this).edit()
                        .putInt("prefAdCounter", ++adCounter).commit();

    }
private boolean connectedToNet() {
    final ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

    if (activeNetwork != null && activeNetwork.isConnected())
        return true;
    else
        return false;
}
boolean isAdClosed = false;


protected void adshow() {

    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.camerar);
    RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.ALIGN_PARENT_RIGHT );

    final AdView adView = new AdView(this);
    adView.setAdUnitId("ca-app-pub-1167514687331347/1312527819");
    adView.setAdSize(AdSize.BANNER);
    mainLayout.addView(adView, rlParams);

    AdRequest adreq = new AdRequest.Builder().build();
    adView.loadAd(adreq);
     }

private void autoCloseAd(){
              new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                   if(isAdClosed){
                                       //AdView adView = new AdView(this);
                                       adView.setVisibility(View.GONE);
                                   isAdClosed = true;
                                   }
                               }
                        }, 3000);
            }    

关于第一个问题,您可以使用计数器在特定时间显示广告。例如,仅在连接到互联网的第 3、6、9、12 次...时加载广告:

int adCounter;

private void loadTimedAd() {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        adCounter = sharedPrefs.getInt("prefAdCounter", 0);
if (adCounter % 3 == 0 && connectedToNet())
            adView.loadAd(new AdRequest.Builder().build());
            else PreferenceManager.getDefaultSharedPreferences(this).edit()
                        .putInt("prefAdCounter", ++adCounter).commit();

    }

//检查互联网是否连接的方法

private boolean connectedToNet() {
        final ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

        if (activeNetwork != null && activeNetwork.isConnected())
            return true;
        else
            return false;
    }

这需要权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

关于您的第二个问题,您可以使用横幅广告的可见性标志在一段时间后自动隐藏广告。例如在一分钟后隐藏广告

boolean isAdClosed = false;

private void autoCloseAd(){
  new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       if(isAdClosed){
                       adView.setVisibility(View.GONE);
                       isAdClosed = true;
                       }
                   }
            }, 60000);
}

然后最后在你的 onCreate() 中调用方法

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    adView = (AdView)findViewById(R.id.ad);
    loadTimedAd();
    autoCloseAd();
}

希望这对您有所帮助。如果您需要进一步的帮助,请告诉我