티스토리 뷰
안드로이드 전면광고 google admob version 20.1.0
build.gradle (project)
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
build.gradle (Module)
implementation 'com.google.android.gms:play-services-ads:20.1.0' 를 추가해주세요. 그리고 Sync now 해주세요.
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.gms:play-services-ads:20.1.0'
}
manifests 에 추가해주세요.
<uses-permission android:name="android.permission.INTERNET"/>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jw.interstitialads_example">
<uses-permission android:name="android.permission.INTERNET"/>
<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/Theme.Interstitialads_example">
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<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>
</manifest>
ca-app-pub-3940256099942544~3347511713는 구글 애드몹 샘플 아이디 입니다.
activity_main.xml 레이아웃 만들기
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/btnshowads"
android:text="ShowAds"
/>
<TextView
android:layout_marginTop="16dp"
android:layout_below="@+id/btnshowads"
android:layout_centerInParent="true"
android:id="@+id/txtShowLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
MainActivity.java에 다음과 같이 추가한다.
package com.jw.interstitialads_example;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private static String AD_ID = "ca-app-pub-3940256099942544/1033173712";
Button button;
TextView txtShowLog;
private String TAG = "MY_TAG";
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
loadAds();
}
});
button = findViewById(R.id.btnshowads);
txtShowLog = findViewById(R.id.txtShowLog);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAds();
}
});
}
private void showAds() {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
private void loadAds() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, AD_ID, adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
loadAds();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
}
});
}
}
2021.06.19 - [프로그래밍/안드로이드 앱프로그래밍] - 안드로이드 보상형 광고 google admob 버젼 20.2.0
안드로이드 애드몹 전면광고동영상 보면서 따라하기
'프로그래밍 > 안드로이드 앱프로그래밍' 카테고리의 다른 글
국가코드 가져와서 TTS에 알맞게 언어 바꾸기 (0) | 2021.06.28 |
---|---|
안드로이드 보상형 광고 google admob 버젼 20.2.0 (0) | 2021.06.19 |
버튼 눌렀을때 메시지가 나타나게 하기 (0) | 2021.06.17 |
안드로이드 sqlite 삭제하기 (Delete) (0) | 2021.04.08 |
안드로이드 SQLite 업데이트(수정하기) (0) | 2021.04.08 |
댓글