我想建立一个可运行的程序,每5秒钟加载一次广告(当然,5秒钟太快了,这只是为了测试目的)。
这是我的代码:
package com.admobsdk_dfp_handler;
import com.google.ads.*;
import com.google.ads.doubleclick.*;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;
public class AdMobSDK_DFP_Handler extends Activity {
private DfpAdView adView;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
adView.loadAd(new AdRequest());
handler.postDelayed(this, 5000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad_mob_sdk__dfp__handler);
adView = new DfpAdView(
this,
AdSize.BANNER,
AD_UNIT_ID);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
handler.postDelayed(runnable, 5000);
};
@Override
protected void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_ad_mob_sdk__dfp__handler,
menu);
return true;
}
}
如果我按下主页按钮将应用隐藏到后台,那么可运行程序将以每5秒的间隔继续加载广告。
是否有任何方法可以在应用程序隐藏到后台时停止可运行程序?非常感谢。
onCreate()
方法之后,接下来会调用onStart()
方法,然后是onResume()
方法。您可以在此处查看详细信息:http://developer.android.com/training/basics/activity-lifecycle/starting.html - user948620