2010-09-20 31 views
4

您好,我在嘗試阻止adMob請求新廣告時遇到問題。如果一個人點擊後退鍵或回家,應用程序會進入睡眠狀態,但admob會繼續請求新的廣告。停止的唯一方法是如果用戶從菜單中選擇退出。AdMob Android在程序不在視圖中請求廣告

有誰知道在我的onPause方法中,我可以做一些事情,比如ad.pauseUpdates();我在文檔中沒有看到類似的東西。

任何想法都會有所幫助。

+0

什麼,當用戶從菜單選擇退出怎麼辦? – 2010-09-21 01:00:52

+0

我只是做了一個super.finish();這需要照顧擺脫應用程序。這有效,請求停止。在暫停中,它會繼續更新。 – JMazApps 2010-09-21 01:09:36

+1

我想我已經解決了它。看來,它仍在請求廣告的原因是因爲我必須重寫onPause和onStop方法。我不明白爲什麼有所作爲,我只是叫super.onPause/onStop應該自動完成,而不會覆蓋它我想?無論哪種方式它似乎工作。如果你能告訴我爲什麼這樣做會變得非常棒!謝謝! – JMazApps 2010-09-21 02:00:09

回答

1

你可以這樣做:

public class BannerSample extends Activity { 
/** The view to show the ad. */ 
    private AdView adView;  

    /* Your ad unit id. Replace with your actual ad unit id. */ 
    private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Create an ad. 
adView = new AdView(this); 
adView.setAdSize(AdSize.BANNER); 
adView.setAdUnitId(AD_UNIT_ID); 

// Add the AdView to the view hierarchy. The view will have no size 
// until the ad is loaded. 
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout); 
layout.addView(adView); 

// Create an ad request. Check logcat output for the hashed device ID to 
// get test ads on a physical device. 
AdRequest adRequest = new AdRequest.Builder() 
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE") 
    .build(); 

// Start loading the ad in the background. 
adView.loadAd(adRequest); 
    } 

    @Override 
    public void onResume() { 
    super.onResume(); 
    if (adView != null) { 
     adView.resume(); 
    } 
    } 

    @Override 
    public void onPause() { 
    if (adView != null) { 
     adView.pause(); 
} 
super.onPause(); 
    } 

/** Called before the activity is destroyed. */ 
    @Override 
    public void onDestroy() { 
// Destroy the AdView. 
if (adView != null) { 
    adView.destroy(); 
    } 
super.onDestroy(); 
} 
    }