google admob接入

在接入admob 的时候先导入jar

implementation 'com.google.android.gms:play-services-ads:17.2.0'
implementation 'com.google.firebase:firebase-core:+'
implementation 'com.google.firebase:firebase-messaging:+'

我在接入得时候只引用admob,发现并没有广告,最后看道firbase 也需要引用,因为和admob是联合使用得。(可能我没找到正确得使用方法),

然后在清单文件中

<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value=""/>

添加引用appid,需要添加,没有添加会报错

然后就是开始接入广告。在接入得时候可以使用他们得测试id,直接使用线上得最后广告是不会返回了

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "gcers";
    private InterstitialAd mInterstitialAd;
    RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //插屏广告
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        handler.sendEmptyMessage(1);
        relativeLayout = findViewById(R.id.res);


        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                Log.i(TAG, "ok===");
                if (mInterstitialAd
                        .isLoaded()){
                    handler.sendEmptyMessage(1);
                }

            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.


                Log.i(TAG, errorCode + "===");
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when the ad is displayed.
            }

            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the interstitial ad is closed.
            }
        });

        handler.sendEmptyMessage(2);

        videos();


    }


    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 1:
                    mInterstitialAd.show();
                    break;
                case 2:
                    init();
                    break;
            }

        }
    };

    private AdRequest adRequest;
    private AdView adView;

    //banner  广告
    public void init() {

        adView = new AdView(this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);


        adView.setLayoutParams(layoutParams);

        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        adView.setAdSize(AdSize.SMART_BANNER);


        adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.CENTER_HORIZONTAL);

        relativeLayout.addView(adView, lp);


    }


    private RewardedVideoAd mRewardedVideoAd;

    //视频广告
    public void video() {
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
            @Override
            public void onRewardedVideoAdLoaded() {
                System.out.println("MainActivity.onRewardedVideoAdLoaded");
                if (mRewardedVideoAd.isLoaded()) {
                    mRewardedVideoAd.show();
                }
            }

            @Override
            public void onRewardedVideoAdOpened() {
                System.out.println("MainActivity.onRewardedVideoAdOpened");

            }

            @Override
            public void onRewardedVideoStarted() {
                System.out.println("MainActivity.onRewardedVideoStarted");

            }

            @Override
            public void onRewardedVideoAdClosed() {
                System.out.println("MainActivity.onRewardedVideoAdClosed");

            }

            @Override
            public void onRewarded(RewardItem rewardItem) {
                System.out.println("MainActivity.onRewarded");

            }

            @Override
            public void onRewardedVideoAdLeftApplication() {
                System.out.println("MainActivity.onRewardedVideoAdLeftApplication");

            }

            @Override
            public void onRewardedVideoAdFailedToLoad(int i) {
                System.out.println("MainActivity.onRewardedVideoAdFailedToLoad");
            }

            @Override
            public void onRewardedVideoCompleted() {
                System.out.println("MainActivity.onRewardedVideoCompleted");

            }
        });

        mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
                new AdRequest.Builder().build());


    }


    public void videos() {

        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                video();
            }
        });
    }

}

这是完整代码。

在使用得时候 ,记得初始化广告

MobileAds.initialize(this, "");

猜你喜欢

转载自blog.csdn.net/a1033479126/article/details/90369568