facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行1: 行1:
 
==公式==
 
==公式==
https://developers.google.com/admob/android/banner?hl=ja
+
https://developers.google.com/ad-manager/mobile-ads-sdk/android/banner?hl=ja
  
 
==レイアウト==
 
==レイアウト==
行21: 行21:
  
  
         <com.google.android.gms.ads.AdView
+
         <com.google.android.gms.ads.doubleclick.PublisherAdView
 
             xmlns:ads="http://schemas.android.com/apk/res-auto"
 
             xmlns:ads="http://schemas.android.com/apk/res-auto"
             android:id="@+id/adView"
+
             android:id="@+id/publisherAdView"
 
             android:layout_width="wrap_content"
 
             android:layout_width="wrap_content"
 
             android:layout_height="wrap_content"
 
             android:layout_height="wrap_content"
行29: 行29:
 
             android:layout_alignParentBottom="true"
 
             android:layout_alignParentBottom="true"
 
             ads:adSize="BANNER"
 
             ads:adSize="BANNER"
             ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
+
             ads:adUnitId="/6499/example/banner">
         </com.google.android.gms.ads.AdView>
+
         </com.google.android.gms.ads.doubleclick.PublisherAdView>
  
 
</RelativeLayout>
 
</RelativeLayout>
 
 
</pre>
 
</pre>
  
 
==サイズ設定とunitId設定==
 
==サイズ設定とunitId設定==
 
<pre>
 
<pre>
AdView adView = new AdView(this);
+
PublisherAdView adView = new PublisherAdView(this);
adView.setAdSize(AdSize.BANNER);
+
adView.setAdSizes(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
+
adView.setAdUnitId("/6499/example/banner");
 
</pre>
 
</pre>
  
 
==ロード処理==
 
==ロード処理==
 
<pre>
 
<pre>
import com.google.android.gms.ads.AdRequest;
+
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.AdView;
+
import com.google.android.gms.ads.doubleclick.PublisherAdView;
  
 
public class MainActivity extends AppCompatActivity {
 
public class MainActivity extends AppCompatActivity {
     private AdView mAdView;
+
     private PublisherAdView mPublisherAdView;
  
 
     protected void onCreate(Bundle savedInstanceState) {
 
     protected void onCreate(Bundle savedInstanceState) {
 
         super.onCreate(savedInstanceState);
 
         super.onCreate(savedInstanceState);
 
         setContentView(R.layout.activity_main);
 
         setContentView(R.layout.activity_main);
         MobileAds.initialize(this, new OnInitializationCompleteListener() {
+
         mPublisherAdView = findViewById(R.id.publisherAdView);
            @Override
+
         PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
            public void onInitializationComplete(InitializationStatus initializationStatus) {
+
         mPublisherAdView.loadAd(adRequest);
            }
+
        });
+
        mAdView = findViewById(R.id.adView);
+
         AdRequest adRequest = new AdRequest.Builder().build();
+
         mAdView.loadAd(adRequest);
+
 
     }
 
     }
 
}
 
}
行68: 行62:
 
==イベント周り==
 
==イベント周り==
 
<pre>
 
<pre>
mAdView.setAdListener(new AdListener() {
+
mPublisherAdView.setAdListener(new AdListener() {
 
     @Override
 
     @Override
 
     public void onAdLoaded() {
 
     public void onAdLoaded() {
行101: 行95:
 
     }
 
     }
 
});
 
});
</pre>
 
  
==admanagerからadmobへ変換==
+
</pre>
*setAdSizes()をsetAdSize()へ
+

2019年10月2日 (水) 19:14時点における版

公式

https://developers.google.com/ad-manager/mobile-ads-sdk/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.doubleclick.PublisherAdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/publisherAdView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="/6499/example/banner">
        </com.google.android.gms.ads.doubleclick.PublisherAdView>

</RelativeLayout>

サイズ設定とunitId設定

PublisherAdView adView = new PublisherAdView(this);
adView.setAdSizes(AdSize.BANNER);
adView.setAdUnitId("/6499/example/banner");

ロード処理

import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;

public class MainActivity extends AppCompatActivity {
    private PublisherAdView mPublisherAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPublisherAdView = findViewById(R.id.publisherAdView);
        PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
        mPublisherAdView.loadAd(adRequest);
    }
}

イベント周り

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