0

我在屏幕上有多个按钮。我想显示Interstitial Ad用户何时单击button以及何时通过意图 Ad Closed进入新的。我该怎么做?与 facebook 观众网络。activity

对于 Admob,这个答案很有帮助 - https://stackoverflow.com/a/47229261/14566306。但这在我们使用 Facebook 受众网络时不起作用。

这是我的代码-

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;

public class page1activity extends AppCompatActivity {
    String TAG = "page1Activity";
    // for fb ads
    private final String TagAd = MainActivity.class.getSimpleName();
    public InterstitialAd interstitialAd;
    
    @Override
    protected void onDestroy() {
        if (interstitialAd != null) {
            interstitialAd.destroy();
        }
        super.onDestroy();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // here call method
        setBtnClickListner();
        // fb ads 
        AudienceNetworkAds.initialize(this);
        funFbAds();
    }

    CardView Btn1Home, Btn2Home, Btn3Home, Btn4Home, Btn5Home,Btn6Home;
    private void setBtnClickListner(){
        Btn1Home =findViewById(R.id.btn1Home);
        Btn1Home.setOnClickListener(this);

        Btn2Home =findViewById(R.id.btn2Home);
        Btn2Home.setOnClickListener(this);

        Btn3Home =findViewById(R.id.btn3Home);
        Btn3Home.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        funIncSfCount();
        switch (v.getId()) {

            case R.id.btn1Home:
                // importance of loan in AC
                Intent intent1btn=new Intent(HomeActivity.this,page1activity.class);
                startActivity(intent1btn);

                break;

            case R.id.btn2Home:
                // What is AC
                Intent intent2btn=new Intent(HomeActivity.this,page2Activity.class);
                startActivity(intent2btn);

                break;

            case R.id.btn3Home:
                // Term and condition for AC
                Intent intent3btn=new Intent(HomeActivity.this,page3ActivityWebview.class);
                startActivity(intent3btn);

                break;

            default:
                break;
        }
    }
    public void funFbAds(){
        interstitialAd = new InterstitialAd(this, "1668688**********14076");
        // Create listeners for the Interstitial Ad
        InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                //interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        };

        // For auto play video ads, it's recommended to load the ad
        // at least 30 seconds before it is shown
        interstitialAd.loadAd(
                interstitialAd.buildLoadAdConfig()
                        .withAdListener(interstitialAdListener)
                        .build());

    }
  
4

1 回答 1

0

您将需要创建一个简单的函数来进行下一个活动

  public void openNextActivity(Class<?> cls){
    Intent intent =new  Intent(this, cls);
    startActivity(intent);
}

然后在监听器中调用它

 @Override
        public void onInterstitialDismissed(Ad ad) {
            // Interstitial dismissed callback
            Log.e(TAG, "Interstitial ad dismissed.");
            openNextActivity(ClassName.class);
        }

您应该只添加一个变量来存储单击按钮的活动并将其传递给函数

于 2021-05-20T20:57:41.150 回答