google admob access

When introduced into the jar prior access admob

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

I have access only when referring to admob, found no ads, look at the last road firbase also need to reference, because admob and are used in combination, too. (I may not have to use to find the right method),

Then in the manifest file

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

Add a reference appid, you need to add, no error will be added

Then access is to begin advertising. You can use the access they have to have time to test id, was the last direct use of online advertising is not returned

 

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();
            }
        });
    }

}

 

This is the complete code.

Was in use, remember to initialize ad

 

MobileAds.initialize(this, "");

Guess you like

Origin blog.csdn.net/a1033479126/article/details/90369568