2010-09-20 32 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(); 
} 
    }