Advertisement
shmilon

Implement Ads With Firebase

Jun 30th, 2022
708
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.os.Bundle;
  2. import android.view.View;
  3. import android.widget.Button;
  4. import android.widget.RelativeLayout;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import com.firebase.client.DataSnapshot;
  7. import com.firebase.client.Firebase;
  8. import com.firebase.client.FirebaseError;
  9. import com.firebase.client.ValueEventListener;
  10. import com.google.android.gms.ads.AdRequest;
  11. import com.google.android.gms.ads.AdSize;
  12. import com.google.android.gms.ads.AdView;
  13. import com.google.android.gms.ads.InterstitialAd;
  14. import com.google.android.gms.ads.MobileAds;
  15. import com.google.android.gms.ads.initialization.InitializationStatus;
  16. import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
  17. public class MainActivity extends AppCompatActivity {
  18.     RelativeLayout banner;
  19.     String data;
  20.     String intVar;
  21.     InterstitialAd interstitialAd;
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         MobileAds.initialize(this, new OnInitializationCompleteListener() {
  28.             @Override
  29.             public void onInitializationComplete(InitializationStatus initializationStatus) {
  30.             }
  31.         });
  32.  
  33.         fireInt("https://testads-.firebaseio.com/inter");
  34.         interstitialAd = new InterstitialAd(this);
  35.  
  36.         Button button = findViewById(R.id.showAds);
  37.         button.setOnClickListener(new View.OnClickListener() {
  38.             @Override
  39.             public void onClick(View v) {
  40.                 if (interstitialAd.isLoaded()){
  41.                     interstitialAd.show();
  42.                 }
  43.  
  44.  
  45.             }
  46.         });
  47.  
  48.         banner = findViewById(R.id.banner_container);
  49.         fireAds("https://testads-.firebaseio.com/banner");
  50.  
  51.     }
  52.  
  53.     public void fireAds(String adsUrl) {
  54.         Firebase.setAndroidContext(this);
  55.         Firebase firebase = new Firebase(adsUrl);
  56.         firebase.addValueEventListener(new ValueEventListener() {
  57.             @Override
  58.             public void onDataChange(DataSnapshot dataSnapshot) {
  59.                 data = dataSnapshot.getValue(String.class);
  60.                 AdView mAdView = new AdView(MainActivity.this);
  61.                 mAdView.setAdUnitId(data);
  62.                 banner.addView(mAdView);
  63.                 mAdView.setAdSize(AdSize.SMART_BANNER);
  64.                 AdRequest adRequest = new AdRequest.Builder().build();
  65.                 mAdView.loadAd(adRequest);
  66.             }
  67.             @Override
  68.             public void onCancelled(FirebaseError firebaseError) {
  69.             }
  70.         });
  71.  
  72.     }
  73.  
  74.  
  75.  
  76.     public void fireInt(String intUrl) {
  77.         Firebase.setAndroidContext(this);
  78.         Firebase firebase = new Firebase(intUrl);
  79.         firebase.addValueEventListener(new ValueEventListener() {
  80.             @Override
  81.             public void onDataChange(DataSnapshot dataSnapshot) {
  82.                 intVar = dataSnapshot.getValue(String.class);
  83.                 interstitialAd.setAdUnitId(intVar);
  84.                 interstitialAd.loadAd(new AdRequest.Builder().build());
  85.  
  86.             }
  87.             @Override
  88.             public void onCancelled(FirebaseError firebaseError) {
  89.             }
  90.         });
  91. }}
Advertisement
RAW Paste Data Copied
Advertisement