Android/広告組込/google/admob/バナーのソースを表示
←
Android/広告組込/google/admob/バナー
ナビゲーションに移動
検索に移動
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
==公式== https://developers.google.com/admob/android/banner?hl=ja ==レイアウト== main_activity.xml <pre> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView> </RelativeLayout> </pre> ==サイズ設定とunitId設定== <pre> AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); </pre> ==ロード処理== <pre> import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { private AdView mAdView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } } </pre> ==イベント周り== <pre> mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdClicked() { // Code to be executed when the user clicks on an ad. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } }); </pre> ==AdManagerからAdMobへ変換== *setAdSizes()をsetAdSize()へ *com.google.android.gms.ads.doubleclick.PublisherAdView を com.google.android.gms.ads.AdView へ *com.google.android.gms.ads.doubleclick.PublisherAdRequest を com.google.android.gms.ads.AdRequest へ *PublisherAdRequestをAdRequestへ *PublisherInterstitialAdをInterstitialAdへ ==タブレットのサイズ== タブレット端末のディスプレイサイズのdisplaysize.y + 1まで広告は表示されるが、displaysize.y + 2で表示されなくなり、"Ad failed to load : 1"のエラーログが発生するようになる。 ディスプレイサイズの定義はここらへん参考に https://akira-watson.com/android/theme-notitlebar.html ==TestDevice== テスト端末として認識させる。idは端末ごとに違う。admobに接続してlogcatで確認できる。 <pre> AdRequest request = new AdRequest.Builder() .addTestDevice("33BE2250B43518CCDA7DE426D04Exxxx") .build(); </pre> or <pre> AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); adRequestBuilder.addTestDevice("33BE2250B43518CCDA7DE426D04xxxx"); AdRequest adRequest = adRequestBuilder.build(); </pre> ==TestDevice(19.0.0以降)== <pre> List<String> testDevices = new ArrayList<>(); testDevices.add(AdRequest.DEVICE_ID_EMULATOR); RequestConfiguration requestConfiguration = new RequestConfiguration.Builder() .setTestDeviceIds(testDevices) .build(); MobileAds.setRequestConfiguration(requestConfiguration); AdRequest.Builder().build(); </pre> 参考:https://stackoverflow.com/questions/60539407/after-updating-google-ads-sdk-addtestdevice-is-deprecated-how-to-resolve =="java.lang.IllegalStateException: The ad size can only be set once on AdView."エラーが出る場合== 2回以上AdSizeを設定しているので、一度入れたら設定しないようにする。 mAdView = new AdView(this); if (mAdView.getAdSize() == null) { mAdView.setAdSize(AdSize.BANNER); } if (mAdView.getAdUnitId() == null) { mAdView.setAdUnitId("ca-app-pub-1234hogehogehogehoge/1234hoge"); }
Android/広告組込/google/admob/バナー
に戻る。
ナビゲーション メニュー
個人用ツール
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
ソースを閲覧
履歴表示
その他
検索
案内
プログラムメモ
php
flutter
java
android
kotlin
ios
unity
unrealengine
javascript
mysql
sqlite
postgresql
oracle
mroonga
mongodb
flash
electron
cocos2dx
titanium
cpp
ruby
perl
python
accessメモ
rss
html
monaca
cordova
golang
blender
セキュリティ
テストツール
サーバメモ
linux
dotnet
apacheメモ
htaccessメモ
subversion
git
仮想サーバ
ansible
sendgrid
xampp
cacti
mecab
faces
flashpolicyd
fcs
jenkins
運用
デザインメモ
css
ユーザビリティ
ux
サービスメモ
twitter
facebook
instagram
mixi
セカンドライフ
通信ログ横取り
google
ustream
aws
gcp
plesk
azure
vps
AI
その他サービス
便利系メモ
SEO
モバイル
抽象変数名
DDD
クライアント
firefox
chrome
pgp
windows
mac
jmetar
Thunderbird
excel
libreoffice
vpnclient
doxygen
VisualStudioCode
fastlane
metaquest
cmsメモ
mediawiki
pukiwiki
wordpress
その他
資格
IT用語
pvを稼ぐ方法
将棋プログラム
その他
ログイン
ページ内
メインページ
最近の更新
人気のページ
問い合わせ
ツール
リンク元
関連ページの更新状況
ページ情報