Using Google AdMob

3

I'm using google admob to place ads in my app, however I have some questions

  • Can I use the same ad unit (id) for multiple activity (each with a block)?
  • How do I make the ad the first thing to load? the idea is that the time it takes the user to close the ad is the time to download the firebase database
  • My code (summarized):

    public class MainActivity extends AppCompatActivity {
    
        private InterstitialAd mInterstitialAd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            MobileAds.initialize(this, "ca-app-pub-5718158120252618~4586568420");
    
            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
            carregarAnuncio();
    
            mInterstitialAd.setAdListener(new AdListener(){
                @Override
                public void onAdClosed() {
                    carregarAnuncio();
                }
            });
    
            banco.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    mInterstitialAd.show();
                    onibus.clear();
                    for(DataSnapshot data: dataSnapshot.getChildren()){
                        BusAdmin b = data.getValue(BusAdmin.class);
                        b.setKey(data.getKey());
    
                        onibus.add(b);
                    }
                    adapter.notifyDataSetChanged();
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
        }
    
        private void carregarAnuncio() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    }
    

    For the time being the ad is only loaded after the bank (firebase) I want the exact opposite

        
    asked by anonymous 14.11.2017 / 20:32

    2 answers

    2

    1st question: No, at least I do not recommend it is good to make separate ad units for each activity, in applications you work with fragments you can place the ad for example along with the container that carries the fragment, but in the case of the interstitial it is better to make separate blocks.

    2nd question: Usually the ad takes a while to be uploaded, this can happen to several factors either because it is a video advertisement or the internet of the user, the solution that I will give you may not be the best but solves your problem, I made the part of the showInterstitial () it opens the interstitial and returns true, returning it loads the code to download the firebase's database.

        public class MainActivity extends AppCompatActivity {
    
    private InterstitialAd mInterstitialAd;
    private boolean boleanloaded;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boleanloaded = false;
    
        MobileAds.initialize(this, "ca-app-pub-5718158120252618~4586568420");
    
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        carregarAnuncio();
    
        mInterstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdClosed() {
                carregarAnuncio();
            }
        });
    
        while (!boleanloaded) {
            boleanloaded = showInterstitial();
            if (boleanloaded)
            banco.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    mInterstitialAd.show();
                    onibus.clear();
                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                        BusAdmin b = data.getValue(BusAdmin.class);
                        b.setKey(data.getKey());
    
                        onibus.add(b);
                    }
                    adapter.notifyDataSetChanged();
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
    
            });}
        }
    
    
    }
    
    private void carregarAnuncio() {
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    }
    
    private boolean showInterstitial() {
    
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            return true;
        } else {
            return false;
        }
    }
    
        
    04.05.2018 / 19:02
    0

    I do not understand AdMob , but to know that the connection to Firebase Database is established, there is a very easy way. It's well explained in the Google development guide under How to detect the connection status from this link . Using the code

    DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
    connectedRef.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
        boolean connected = snapshot.getValue(Boolean.class);
        if (connected) {
          System.out.println("connected");
        } else {
          System.out.println("not connected");
        }
      }
    
      @Override
      public void onCancelled(DatabaseError error) {
        System.err.println("Listener was cancelled");
      }
    });
    

    Putting a while(!connected){...} I think it solves your problem. Ps: Remember to use the database reference in the root of your database as in the example, entering a child will not work.

        
    09.05.2018 / 04:04