Android : AdMob onClickListener

后端 未结 4 787
忘掉有多难
忘掉有多难 2021-01-03 03:15

I display into my android application AdMob\'s banners. I would like that when the user click on the banner it gone. I have try the code AdView.setOnC

4条回答
  •  被撕碎了的回忆
    2021-01-03 03:47

    I agree with above all the answer but i am just extending the answer. I have done like below

    Load the ad in AdView as below

    AdView adView = (AdView)findViewById(R.id.adView);
    adView.loadAd(new AdRequest.Builder()
                        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                        .addTestDevice("your device id")
                        .build());
    

    Now next set the ad listener on that adView. Take one global boolean variable like isClicked so whenever user will click on ad and switch on that page onAdLeftApplication() method will called so make that variable as true. When user will click on ad then redirect to that ad page so current app will goes to onPause state.

    adView.setAdListener(adListener);
    
    com.google.android.gms.ads.AdListener adListener = new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
            }
    
            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
            }
    
            @Override
            public void onAdLeftApplication() {
                super.onAdLeftApplication();
                isClicked = true;
            }
    
            @Override
            public void onAdOpened() {
                super.onAdOpened();
            }
    
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
            }
    
            @Override
            public void onAdClicked() {
                super.onAdClicked();
            }
    
            @Override
            public void onAdImpression() {
                super.onAdImpression();
            }
        };
    

    Now whenever user will come back to the current app activity onResume() method is called, So implement your other stuff will be there or as below

    @Override
        protected void onResume() {
            super.onResume();
            if (isClicked){
                isClicked = false;
                // do your other stuff whatever you want;
            }
        }
    

提交回复
热议问题