2013-07-10 24 views
2

我正在開發一個android應用,並希望在用戶退出應用後按按鈕時顯示mopub插頁式全屏廣告。Android:顯示mopub插頁式廣告,當用戶退出應用後點擊返回按鈕

我已經嘗試過創建插頁式廣告並將其顯示在onDestroy方法中。類似的東西:

@Override 
public void onDestroy(){ 
    this.interstitial = new MoPubInterstitial(this, MY_INTERSTITIAL_AD_UNIT_ID_HERE); 
    this.interstitial.setInterstitialAdListener(this); 
    this.interstitial.load(); 

    super.onDestroy(); 
} 

// InterstitialAdListener method 
@Override 
public void onInterstitialLoaded(MoPubInterstitial interstitial) { 
    if (interstitial.isReady()) { 
     mInterstitial.show(); 
    } else { 
     // Other code 
    } 
} 

不過,我不破壞任何地方間(mInterstitial.destroy();),因爲我不知道我在哪裏可以做到這一點,因此我得到這個錯誤:

Activity com.myActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
android.app.IntentReceiverLeaked: Activity com.myActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 

雖然我得到這個錯誤,添加顯示(我已經在很多設備上測試過),它似乎在除了索尼以外的所有工作。

我該如何改進此代碼以在退出時顯示插頁式廣告?

謝謝!

回答

1

您在ondestroy時加載插頁式廣告。當您的活動在您泄漏的插頁式廣告載入後遭到破壞時。在ondestroy調用interstitial.destroy()。

你可能想要做的是處理onbackpressed。加載interstitial oncreate,顯示它onbackpressed並銷燬它在銷燬。

+0

我已經用我使用的解決方案回答了問題,但也許您的答案也可能有效。非常感謝你。 – ljmelgui

0

在的onDestroy(),請確保你摧毀mopubview

if (mMoPubView!=null) { 
     mMoPubView.destroy(); 
    } 
0

你可以嘗試重載onBackPressed方法來代替。 這樣,onDestroy仍然可以用於清理間隙。

在活動

@Override 
public void onBackPressed(){ 
    if (interstitial.isReady()) { 
     interstitial.show(); //this will show the interstitial if it's ready 
    } else { 
     super.onBackPressed(); //this will exit the program 
    } 
} 

然後在監聽通話結束()時返回間質性

(故障時或接近)

@Override 
public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) { 
    finish(); //this will exit the program if interstitial fail to load. 
} 

@Override 
public void onInterstitialDismissed(MoPubInterstitial interstitial) { 
    finish(); //this will exit the program if interstitial is closed 
} 
當然

你仍然需要調用摧毀平常。

@Override 
protected void onDestroy() { 
    interstitial.destroy(); 
    super.onDestroy(); 
} 
1

舊帖子,但我正在尋找這個問題,並找到這個職位。現在

,如果你得到一個錯誤約「活動已泄漏窗口」,記得把完成()上onAdClosed和無處

mInterstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdClosed() { 
       finish(); 
       requestNewInterstitial(); 

      } 
     }); 

此,如果你想接近的活動和展示間。

0

100%的工作解決方案,我們需要調用間諜廣告和橫幅廣告的銷燬功能。

@Override 
    protected void onDestroy() { 

     if(mopubInterstitial != null){ 
      mopubInterstitial.destroy(); 
     } 
     if (mopubBannerPubView != null){ 
      mopubBannerPubView.destroy(); 
     } 
     super.onDestroy(); 
    } 
相關問題