2017-02-17 29 views
0

我向我的應用添加了插頁式廣告(+橫幅),其中包含一項活動,橫幅沒有問題,但插頁式廣告僅在應用啓動時顯示一次,我的應用不包含任何按鈕或操作,僅包含pdfview,想要做的就是當用戶閱讀pdf時每5分鐘顯示一次廣告。我搜索了類似的問題,但無法得到它。在這裏我的代碼加載和顯示廣告:如何調用多個插頁式廣告?

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

    // Prepare the Interstitial Ad 
    interstitial = new InterstitialAd(PDFViewActivity.this); 
    // Insert the Ad Unit ID 
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); 
    interstitial.loadAd(adRequest); 
    // Prepare an Interstitial Ad Listener 
    interstitial.setAdListener(new AdListener() { 
     public void onAdLoaded() { 
      // Call displayInterstitial() function 
      displayInterstitial(); 
     } 
    }); 
} 

public void displayInterstitial() { 
// If Ads are loaded, show Interstitial else show nothing. 
    if (interstitial.isLoaded()) { 
     interstitial.show(); 
    } 
} 
+0

想想這個想法兩次。首先,這不是Google提出的展示插頁式廣告的建議方式。其次,你會因爲這種惱人的行爲而讓用戶失望,所以他們絕不會點擊並卸載你的應用。 – Bevor

+0

如果你認爲你比用戶更瞭解它,那麼祝你好運;) – Bevor

回答

0

我就是這麼做的:

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

    // Prepare the Interstitial Ad 
    interstitial = new InterstitialAd(this); 
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); 

    interstitial.loadAd(adRequest); 

    interstitial.setAdListener(new AdListener() { 
     public void onAdLoaded() { 
      displayInterstitial(); 
     } 
     public void onAdClosed() { 
      requestNewInterstitial(); 
     } 

    }); 
} 

public void displayInterstitial() { 
// If Ads are loaded, show Interstitial else show nothing. 
    if (interstitial.isLoaded()) { 
     interstitial.show(); 
    } 
} 

public void requestNewInterstitial() { 
    mHandler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      AdRequest adRequest = new AdRequest.Builder() 
        .build(); 
      interstitial.loadAd(adRequest); 

      if (mHandler != null) { 
       mHandler.postDelayed(this, 100000); //time (ms) 
      } 
     } 
    }, 100000); //time (ms) 
} 
0

好了,你想在規則的時間間隔來顯示插頁廣告。那麼你可以通過使用下面的代碼行來做到這一點。

但是,這是違反Google admob的政策。但是,你走了。

在mainactiviy.java中添加以下代碼。

prepareAd(); 

    ScheduledExecutorService scheduler = 
      Executors.newSingleThreadScheduledExecutor(); 
    scheduler.scheduleAtFixedRate(new Runnable() { 

     public void run() { 
      Log.i("hello", "world"); 
      runOnUiThread(new Runnable() { 
       public void run() { 
        if (mInterstitialAd.isLoaded()) { 
         mInterstitialAd.show(); 
        } else { 
         Log.d("TAG"," Interstitial not loaded"); 
        } 

        prepareAd(); 


       } 
      }); 

     } 
    }, 20, 20, TimeUnit.SECONDS); 

然後MainActivity.java

public void prepareAd(){ 
    mInterstitialAd = new InterstitialAd(this); 
    mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx"); 
    mInterstitialAd.loadAd(new AdRequest.Builder().build()); 
} 

public void prepareAd(){ 
    mInterstitialAd = new InterstitialAd(this); 
    mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx"); 
    mInterstitialAd.loadAd(new AdRequest.Builder().build()); 
} 

間質性AdMob編號這裏使用的是一個樣品ID的最後大括號之前添加下面的行代碼。代碼取自http://www.elegantespace.com/how-to-load-interstitial-ad-at-regular-interval-of-time/