2017-03-08 79 views
0

我的應用程序在應用程序中顯示插頁式廣告,有時在應用程序因Internet速度較慢而關閉後顯示。在應用關閉後展示插頁式廣告違反了admob規則。如何阻止我的插頁式廣告在應用關閉後顯示廣告?如何停止應用退出後顯示的插頁式廣告?

public class des extends AppCompatActivity { 
 

 
    ImageButton imageView15; 
 
    InterstitialAd mInterstitialAd; 
 
    private InterstitialAd interstitial; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_des); 
 

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

 
        // Prepare the Interstitial Ad 
 
     interstitial = new InterstitialAd(des.this); 
 
// Insert the Ad Unit ID 
 
     interstitial.setAdUnitId(getString(R.string.intertitial_id)); 
 

 
     interstitial.loadAd(adRequest); 
 
// Prepare an Interstitial Ad Listener 
 
     interstitial.setAdListener(new AdListener() { 
 
      public void onAdLoaded() { 
 
       // Call displayInterstitial() function 
 
       displayInterstitial(); 
 
      } 
 
     }); 
 

 

 
     imageView15 = (ImageButton) findViewById(R.id.imageView15); 
 
     imageView15.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       Intent intentLoadNewActivity = new Intent(des.this,vid.class); 
 
       startActivity(intentLoadNewActivity); 
 
      } 
 
     }); 
 

 

 
    } 
 

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

 

 
}

回答

1

你需要銷燬活動結束或(退出應用程序)廣告

試試這個

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    if (mInterstitialAd != null) { 
     mInterstitialAd.destroy(); 
    } 
} 
+0

您可以編輯我的Java代碼與任何的onDestroy服務類(),因爲當我嘗試它不是工作摧毀字變成紅色,我不知道如何修復它我trired ALT +進入即時通訊新的應用程序開發 –

+0

您需要重寫onDestroy()方法快捷鍵(Ctrl + O) –

+0

嘗試運行沒有如果塊只是銷燬方法 –

2

的問題是,你是顯示的加入OnAdLoaded,這是不建議的。當用戶正在做其他工作時,這會導致不良行爲和攔截。您應該在交流時加載您的廣告tivity開始,然後你應該在沒有檢查用戶是否被加載的情況下顯示它。 這樣你的廣告就不會在用戶完成你的應用程序後顯示,你的問題將得到解決。

0

您可以跟蹤活動的生命週期,然後在展示廣告之前檢查活動是否正在運行。

  1. 創建一個實例變量:

    private boolean isRunning; 
    
  2. 跟蹤活動的生命週期:

    public void displayInterstitial() { 
        // If Ads are loaded and the activity is running, show Interstitial else show nothing. 
        if (isRunning && interstitial.isLoaded()) { 
         interstitial.show(); 
        } 
    } 
    

    @Override 
    protected void onStart() { 
        super.onStart(); 
        isRunning = true; 
    } 
    
    @Override 
    protected void onStop() { 
        super.onStop(); 
        isRunning = false; 
    } 
    
  3. 檢查活動是展示廣告前運行

+0

它工作!多謝 –

0

或者,你可以使用:system.exit(0);每當用戶希望只要退出你的應用程序爲您的應用程序不包含

相關問題