개발

안드로이드 앱 Admob 광고 추가 예제

개미v 2018. 12. 1. 13:13

 

구글 광고배너인 Admob을 안드로이드 앱에 추가하는 예제 입니다.

 

1. build.gradle(app)에 추가

1
2
3
dependencies {
    implementation 'com.google.android.gms:play-services-ads:17.1.1'
}
cs

 

2. Layout 수정

1
2
3
4
5
6
7
8
9
10
<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>
cs

※ 발급 받은 Unit ID를 입력해 주세요.

 

3. Activity 수정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
 
public class MainActivity extends AppCompatActivity {
 
    private AdView adView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // 애드몹 초기화 (AdMob app ID)
        MobileAds.initialize(this"ca-app-pub-3940256099942544~3347511713");
 
        adView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
 
        // 광고 불러오기
        adView.loadAd(adRequest);
 
    }
}
 
cs

※ 발급 받은 Application ID를 입력해 주세요.

 

4. manifests에 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- 인터넷 권한 등록 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
 
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713"/>
 
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
cs

※발급 받은 Application ID를 입력해 주세요.

 

■ 프로그램 실행 화면

 

 전체 소스

AdmobExample.zip