admob 設定
public class MainActivity extends AppCompatActivity {
private String T = "### MainActivity";
private MySurfaceView3 mySurfaceView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySurfaceView = new MySurfaceView3(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
addContentView(mySurfaceView, rl);
Google_admob();
View view = findViewById(R.id.activity_main);
view.bringToFront();
}
protected void Google_admob() {
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
ViewGroup viewGroup = findViewById(R.id.activity_main);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adView.setLayoutParams(layoutParams);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice("7026FA2EC1DC7E60FBEA02C64D33BD8B");
builder.addTestDevice("53185CF5BFA5B2121DF7FA86E7064C22");
AdRequest adRequest = builder.build();
adView.loadAd(adRequest);
viewGroup.addView(adView);
adView.setAdListener(new AdListener() {
@Override public void onAdLoaded() {
super.onAdLoaded();
Log.i(T, "onAdLoaded");
}
@Override public void onAdOpened() {
super.onAdOpened();
Log.i(T, "onAdOpened");
}
@Override public void onAdLeftApplication() {
super.onAdLeftApplication();
Log.i(T, "onAdLeftApplication");
}
@Override public void onAdClosed() {
super.onAdClosed();
Log.i(T, "onAdClosed");
}
});
}
}
//
I / ### MainActivity: onAdLoaded
I / ### MainActivity: onAdOpened
I / ### MainActivity: onAdLeftApplication
I / ### MainActivity: onAdClosed
I / ### MainActivity: onAdLoaded