admob 广告分析

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查看布局
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android逆向小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值