با مجموعهها، منظم بمانید
ذخیره و دستهبندی محتوا براساس اولویتهای شما.
تبلیغات بینابینی تبلیغات تمام صفحه ای هستند که رابط برنامه میزبان خود را پوشش می دهند. آنها معمولاً در نقاط انتقال طبیعی در جریان یک برنامه نمایش داده می شوند، مانند بین فعالیت ها یا در طول مکث بین سطوح در یک بازی. هنگامی که یک برنامه یک تبلیغ بینابینی را نشان می دهد، کاربر این انتخاب را دارد که یا روی تبلیغ ضربه زده و به مقصد خود ادامه دهد یا آن را ببندد و به برنامه بازگردد.مطالعه موردی .
این راهنما نحوه ادغام تبلیغات بینابینی را در یک برنامه اندروید توضیح می دهد.
هنگام ساخت و آزمایش برنامه های خود، مطمئن شوید که از تبلیغات آزمایشی به جای تبلیغات زنده و تولیدی استفاده می کنید. عدم انجام این کار می تواند منجر به تعلیق حساب شما شود.
سادهترین راه برای بارگیری آگهیهای آزمایشی، استفاده از شناسه واحد آگهی آزمایشی اختصاصی ما برای محتوای بینافزاری Android است:
ca-app-pub-3940256099942544/1033173712
این بهطور ویژه پیکربندی شده است تا تبلیغات آزمایشی را برای هر درخواست بازگرداند، و شما میتوانید هنگام کدنویسی، آزمایش و اشکالزدایی از آن در برنامههای خود استفاده کنید. فقط مطمئن شوید که قبل از انتشار برنامه خود، آن را با شناسه واحد تبلیغاتی خود جایگزین کنید.
برای کسب اطلاعات بیشتر در مورد نحوه عملکرد تبلیغات آزمایشی SDK Ads Mobile، به Test Ads مراجعه کنید.
یک تبلیغ را بارگیری کنید
برای بارگیری یک تبلیغ بینابینی، متد InterstitialAd static load() را فراخوانی کنید و یک InterstitialAdLoadCallback را ارسال کنید تا آگهی بارگذاری شده یا هر گونه خطای احتمالی را دریافت کنید. توجه داشته باشید که مانند سایر فرمتهای بازخوانی بارگذاری، InterstitialAdLoadCallback از LoadAdError برای ارائه جزئیات خطای وفاداری بالاتر استفاده میکند.
FullScreenContentCallback رویدادهای مربوط به نمایش InterstitialAd شما را مدیریت می کند. قبل از نمایش InterstitialAd ، مطمئن شوید که پاسخ تماس را تنظیم کرده اید:
تبلیغات بینابینی باید در طول مکث های طبیعی در جریان برنامه نمایش داده شوند. بین سطوح یک بازی مثال خوبی است، یا پس از اتمام کار کاربر. برای نمایش یک بینابینی، از متد show() استفاده کنید.
در نظر بگیرید که آیا تبلیغات بینابینی نوع مناسبی از تبلیغات برای برنامه شما هستند یا خیر.
تبلیغات بینابینی در برنامه هایی با نقاط انتقال طبیعی بهترین کارایی را دارند. نتیجه گیری یک کار در یک برنامه، مانند اشتراک گذاری یک تصویر یا تکمیل یک سطح بازی، چنین نقطه ای را ایجاد می کند. از آنجایی که کاربر انتظار یک وقفه در عمل را دارد، ارائه یک تبلیغ بینابینی بدون ایجاد اختلال در تجربه او آسان است. مطمئن شوید که در نظر داشته باشید که در چه نقاطی از گردش کار برنامه خود تبلیغات بینابینی را نمایش می دهید و احتمالاً کاربر چگونه پاسخ می دهد.
به یاد داشته باشید که هنگام نمایش یک تبلیغ بینابینی، اقدام را متوقف کنید.
انواع مختلفی از تبلیغات بینابینی وجود دارد: متن، تصویر، ویدیو و موارد دیگر. مهم است که مطمئن شوید وقتی برنامه شما یک تبلیغ بینابینی را نمایش میدهد، استفاده از برخی منابع را نیز به حالت تعلیق در میآورد تا تبلیغ بتواند از آنها استفاده کند. به عنوان مثال، هنگامی که برای نمایش یک تبلیغ بینابینی تماس می گیرید، مطمئن شوید که خروجی صوتی تولید شده توسط برنامه شما را متوقف کنید.
زمان بارگذاری کافی را در نظر بگیرید.
همانطور که اطمینان از نمایش تبلیغات بینابینی در زمان مناسب بسیار مهم است، اطمینان از اینکه کاربر مجبور نیست منتظر بارگذاری آنها بماند نیز مهم است. بارگیری آگهی از قبل با فراخوانی load() قبل از اینکه قصد تماس با show() را داشته باشید، می تواند اطمینان حاصل کند که برنامه شما یک آگهی بینابینی کاملاً بارگذاری شده در زمان نمایش آن آماده است.
کاربر را با تبلیغات غرق نکنید.
در حالی که افزایش تعداد تبلیغات بینابینی در برنامه شما ممکن است راهی عالی برای افزایش درآمد به نظر برسد، همچنین می تواند تجربه کاربر را کاهش دهد و نرخ کلیک را کاهش دهد. اطمینان حاصل کنید که کاربران آنقدر مکرراً قطع نمی شوند که دیگر نتوانند از استفاده از برنامه شما لذت ببرند.
کد منبع
MyActivity.java
/*
* Copyright (C) 2013 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.gms.example.interstitialexample;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
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;
/**
* Main Activity. Inflates main activity xml.
*/
@SuppressLint("SetTextI18n")
public class MyActivity extends AppCompatActivity {
private static final long GAME_LENGTH_MILLISECONDS = 3000;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
private static final String TAG = "MyActivity";
private InterstitialAd interstitialAd;
private CountDownTimer countDownTimer;
private Button retryButton;
private boolean gameIsInProgress;
private long timerMilliseconds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Log the Mobile Ads SDK version.
Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion());
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
loadAd();
// Create the "retry" button, which tries to show an interstitial between game plays.
retryButton = findViewById(R.id.retry_button);
retryButton.setVisibility(View.INVISIBLE);
retryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInterstitial();
}
});
startGame();
}
public void loadAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
this,
AD_UNIT_ID,
adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
MyActivity.this.interstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
Toast.makeText(MyActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
MyActivity.this.interstitialAd = null;
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
MyActivity.this.interstitialAd = null;
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d("TAG", "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
interstitialAd = null;
String error =
String.format(
"domain: %s, code: %d, message: %s",
loadAdError.getDomain(), loadAdError.getCode(), loadAdError.getMessage());
Toast.makeText(
MyActivity.this, "onAdFailedToLoad() with error: " + error, Toast.LENGTH_SHORT)
.show();
}
});
}
private void createTimer(final long milliseconds) {
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
if (countDownTimer != null) {
countDownTimer.cancel();
}
final TextView textView = findViewById(R.id.timer);
countDownTimer = new CountDownTimer(milliseconds, 50) {
@Override
public void onTick(long millisUnitFinished) {
timerMilliseconds = millisUnitFinished;
textView.setText("seconds remaining: " + ((millisUnitFinished / 1000) + 1));
}
@Override
public void onFinish() {
gameIsInProgress = false;
textView.setText("done!");
retryButton.setVisibility(View.VISIBLE);
}
};
}
@Override
public void onResume() {
// Start or resume the game.
super.onResume();
if (gameIsInProgress) {
resumeGame(timerMilliseconds);
}
}
@Override
public void onPause() {
// Cancel the timer if the game is paused.
countDownTimer.cancel();
super.onPause();
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (interstitialAd != null) {
interstitialAd.show(this);
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
startGame();
}
}
private void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (interstitialAd == null) {
loadAd();
}
retryButton.setVisibility(View.INVISIBLE);
resumeGame(GAME_LENGTH_MILLISECONDS);
}
private void resumeGame(long milliseconds) {
// Create a new timer for the correct length and start it.
gameIsInProgress = true;
timerMilliseconds = milliseconds;
createTimer(milliseconds);
countDownTimer.start();
}
}
MainActivity.kt
package com.google.android.gms.example.interstitialexample
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.*
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.google.android.gms.example.interstitialexample.databinding.ActivityMainBinding
const val GAME_LENGTH_MILLISECONDS = 3000L
const val AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712"
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var interstitialAd: InterstitialAd? = null
private var countdownTimer: CountDownTimer? = null
private var gameIsInProgress = false
private var adIsLoading: Boolean = false
private var timerMilliseconds = 0L
private var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// Log the Mobile Ads SDK version.
Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion())
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this) {}
// Set your test devices. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device."
MobileAds.setRequestConfiguration(
RequestConfiguration.Builder().setTestDeviceIds(listOf("ABCDEF012345")).build()
)
// Create the "retry" button, which triggers an interstitial between game plays.
binding.retryButton.visibility = View.INVISIBLE
binding.retryButton.setOnClickListener { showInterstitial() }
// Kick off the first play of the "game."
startGame()
}
private fun loadAd() {
var adRequest = AdRequest.Builder().build()
InterstitialAd.load(
this,
AD_UNIT_ID,
adRequest,
object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.message)
interstitialAd = null
adIsLoading = false
val error =
"domain: ${adError.domain}, code: ${adError.code}, " + "message: ${adError.message}"
Toast.makeText(
this@MainActivity,
"onAdFailedToLoad() with error $error",
Toast.LENGTH_SHORT
)
.show()
}
override fun onAdLoaded(ad: InterstitialAd) {
Log.d(TAG, "Ad was loaded.")
interstitialAd = ad
adIsLoading = false
Toast.makeText(this@MainActivity, "onAdLoaded()", Toast.LENGTH_SHORT).show()
}
}
)
}
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
private fun createTimer(milliseconds: Long) {
countdownTimer?.cancel()
countdownTimer =
object : CountDownTimer(milliseconds, 50) {
override fun onTick(millisUntilFinished: Long) {
timerMilliseconds = millisUntilFinished
binding.timer.text = "seconds remaining: ${ millisUntilFinished / 1000 + 1 }"
}
override fun onFinish() {
gameIsInProgress = false
binding.timer.text = "done!"
binding.retryButton.visibility = View.VISIBLE
}
}
}
// Show the ad if it's ready. Otherwise toast and restart the game.
private fun showInterstitial() {
if (interstitialAd != null) {
interstitialAd?.fullScreenContentCallback =
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
Log.d(TAG, "Ad was dismissed.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
interstitialAd = null
loadAd()
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
Log.d(TAG, "Ad failed to show.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
interstitialAd = null
}
override fun onAdShowedFullScreenContent() {
Log.d(TAG, "Ad showed fullscreen content.")
// Called when ad is dismissed.
}
}
interstitialAd?.show(this)
} else {
Toast.makeText(this, "Ad wasn't loaded.", Toast.LENGTH_SHORT).show()
startGame()
}
}
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
private fun startGame() {
if (!adIsLoading && interstitialAd == null) {
adIsLoading = true
loadAd()
}
binding.retryButton.visibility = View.INVISIBLE
resumeGame(GAME_LENGTH_MILLISECONDS)
}
private fun resumeGame(milliseconds: Long) {
// Create a new timer for the correct length and start it.
gameIsInProgress = true
timerMilliseconds = milliseconds
createTimer(milliseconds)
countdownTimer?.start()
}
// Resume the game if it's in progress.
public override fun onResume() {
super.onResume()
if (gameIsInProgress) {
resumeGame(timerMilliseconds)
}
}
// Cancel the timer if the game is paused.
public override fun onPause() {
countdownTimer?.cancel()
super.onPause()
}
}