Android AdMob広告
build.gradle
dependencies {
...
implementation 'com.google.android.gms:play-services-ads:11.8.0'
}
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
protected void admo() {
// xmlns:ads="http://schemas.android.com/apk/res-auto"
ViewGroup 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.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
adView.setLayoutParams(layoutParams);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
viewGroup.addView(adView);
}
https://developers.google.com/admob/android/banner
Set Up Google Play Services
https://developers.google.com/android/guides/setup
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice("7026FA2EC1DC7E60FBEA02C64D33BD8B"); //asus
adRequest.addTestDevice("53185CF5BFA5B2121DF7FA86E7064C22"); //huawei
adView.loadAd(adRequest.build());
<manifest>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="[ADMOB_APP_ID]"/>
</application>
</manifest>
public class MainActivity extends AppCompatActivity {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
}
...
}
https://developers.google.com/admob/android/test-ads
Enable test devices