1、测试广告集成,官方文档 https://developers.google.com/admob/android/quick-start?hl=zh-cn
dependencies {
implementation("com.google.android.gms:play-services-ads:23.3.0")
}
2、广告集成,集成测试激励广告。
public class MainActivity extends AppCompatActivity {
String TAG = "admob";
private RewardedAd rewardedAd;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
context = this;
new Thread(() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
}).start();
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(context, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
rewardedAd = ad;
Log.d(TAG, "Ad was loaded.");
if (rewardedAd != null) {
Activity activityContext = MainActivity.this;
rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.");
}
rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
}
});
}
});
}
}
3、查看广告activity
adb shell dumpsys avtivity top
TASK 10266:com.mycode.admobdemo id=73 userId=0
ACTIVITY com.mycode.admobdemo/com.google.android.gms.ads.AdActivity 90fc91c pid=13407
...
View Hierarchy:
DecorView@a5b4167[AdActivity]
android.widget.LinearLayout{df84975 V.E...... ........ 0,0-1080,2088}
android.view.ViewStub{d73fddf G.E...... ......I. 0,0-0,0 #10201b3 android:id/action_mode_bar_stub}
android.widget.FrameLayout{66808ac V.E...... ........ 0,0-1080,2088 #1020002 android:id/content}
com.google.android.gms.ads.internal.overlay.h{fc4f65f V.E...... ........ 0,0-1080,2088 #3e8}
com.google.android.gms.ads.internal.webview.ab{88e3228 V.E...... ........ 0,0-1080,2088}
com.google.android.gms.ads.internal.video.s{b373a2c V.E...... ........ 0,0-1080,2088}
android.widget.FrameLayout{4455cf5 V.E...... ........ 0,594-1080,1204}
com.google.android.gms.ads.internal.video.exoplayer.n{e596a8a V.ED..... ........ 0,1-1080,608}
com.google.android.gms.ads.internal.webview.ai{b4498fb VFEDHVC.. .F...... 0,0-1080,2088 aid=2}
android.widget.TextView{ff3cd18 V.ED..... ........ 465,0-614,56}
com.google.android.gms.ads.internal.overlay.r{bdbd371 VFE...C.. ........ 0,0-0,0}
android.widget.ImageButton{16ba556 GFED..C.. ......I. 0,0-0,0}
android.view.View{e971d0a V.ED..... ........ 0,2088-1080,2220 #1020030 android:id/navigationBarBackground}
4、Layout Inspector查看布局