안드로이드 구글 애드몹 배너광고 달기

제가 제작했던 앱 중에 구글 애드몹 광고로 변경해야 하는 것이 있어서 배너 광고를 추가할 수 있는 방법이 무엇인지 알아보려고 합니다. 생각하시는 것 보다 어렵지 않게 설치가 가능하니까 잘 확인해주세요.


애드몹 배너 광고 설치

먼저 구글 애드몹 사이트에서 배너 광고를 만들어주세요. 광고가 생성되어 있어야 광고 단위 id를 받고 바로 앱에 추가할 수 있습니다.

위와 같이 애드몹 사이트에서 배너 광고를 생성하게되면 아래에 조그만한 글씨로 광고 단위 id가 적혀있는 것을 확인하실 수 있을거예요. 이제 배너 광고는 준비가 끝났으니 광고를 설치해보겠습니다.


앱 이름을 수정할 수 있는 values 폴더에 strings.xml 파일을 열어서 광고 단위 id를 입력하세요.


<string name="app_name">에버플래닛 스킬도우미</string>

<string name="banner_ad_unit_id">ca-app-pub-000/000</string>


다음은 build.gradle 파일을 열어서 구글 플레이 서비스 코드를 추가하도록 하겠습니다.


dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.google.android.gms:play-services:8.3.0'


이제는 AndroidManifest.xml 파일에서 애드몹 광고 관련 코드를 입력해줄게요.


<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<application

android:allowBackup="true"

android:icon="@mipmap/icon"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme"

android:screenOrientation="portrait">

<activity android:name=".MainActivity"

android:screenOrientation="portrait">

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<meta-data android:name="com.google.android.gms.version"

android:value="@integer/google_play_services_version"/>

<activity android:name="com.google.android.gms.ads.AdActivity"

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

android:theme="@android:style/Theme.Translucent" />

<activity android:name=".Splash" android:screenOrientation="portrait"/>

</application>


이제는 res 폴더에서 layout 폴더로 이동하여 광고가 보여질 xml 파일에 코드를 추가합니다.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:ads="http://schemas.android.com/apk/res-auto"

   android:layout_width="match_parent"

   android:layout_height="match_parent">

   <com.google.android.gms.ads.AdView

      android:id="@+id/adView"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_centerHorizontal="true"

      android:layout_alignParentBottom="true"

      ads:adSize="SMART_BANNER"

      ads:adUnitId="@string/banner_ad_unit_id">

   </com.google.android.gms.ads.AdView>

</RelativeLayout>


이제 거의 끝나가는군요. 마지막으로 activity 파일에서 코드를 입력해주면 앱을 실행했을 때 광고가 보여지게 됩니다.


public class MainActivity extends Activity {

   AdView mAdView;   


   @Override

   public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


      mAdView = (AdView) findViewById(R.id.adView);

      AdRequest adRequest = new AdRequest.Builder().addTestDevice("611EC7D7AEF5D03ED341D2EFA15589F3").build();

      mAdView.loadAd(adRequest);    

   }

   @Override

   public void onPause() {

      if (mAdView != null) {

         mAdView.pause();

      }

      super.onPause();

   }

   @Override

   public void onResume() {

      if (mAdView != null) {

         mAdView.resume();

      }

      super.onResume();

   }

   @Override

   public void onDestroy() {

      if (mAdView != null) {

         mAdView.destroy();

      }

      super.onDestroy();

   }

}


activity 파일에 입력된 코드를 보시면 addTestDevice에 611EC7D7AEF5D03ED341D2EFA15589F3가 입력되어 있는데 이 코드는 제가 테스트하는 기기의 넘버라서 여러분들의 코드로 입력하셔야 합니다. addTestDevice 코드를 입력하셔야 내 광고를 내가 실수로 터치하는 상황이 발생하지 않기 때문에 필수로 추가해야 합니다.

추가로 자세한 내용을 확인하고 싶은 분들은 https://developers.google.com/admob/android/quick-start 링크를 클릭해서 이동하시면 광고 추가와 관련된 내용을 자세하게 확인하실 수 있습니다.

반응형

이 글을 공유하기

댓글

Designed by JB FACTORY