facebook twitter hatena line google mixi email

「Android/広告組込/google/admob/バナー」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(レイアウト)
(admanagerからadmobへ変換)
行100: 行100:
 
</pre>
 
</pre>
  
==admanagerからadmobへ変換==
+
==AdManagerからAdMobへ変換==
 
*setAdSizes()をsetAdSize()へ
 
*setAdSizes()をsetAdSize()へ
 
*com.google.android.gms.ads.doubleclick.PublisherAdView を com.google.android.gms.ads.AdView へ
 
*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 へ
 
*com.google.android.gms.ads.doubleclick.PublisherAdRequest を com.google.android.gms.ads.AdRequest へ

2019年10月7日 (月) 10:31時点における版

公式

https://developers.google.com/admob/android/banner?hl=ja

レイアウト

main_activity.xml

<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>

サイズ設定とunitId設定

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

ロード処理

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);
    }
}

イベント周り

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.
    }
});

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 へ