Use of Google Admob

Asked

Viewed 216 times

3

I am using google admob to place ads in my app, but I have some questions

  1. I can use the same ad block (id) for multiple Activity (each with a block)?
  2. 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 now the ad is only loaded after the bank (firebase) want just the opposite

2 answers

2


1st question: No, at least I do not recommend it is good to make separate ad blocks 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 interstitial it is better to make separate blocks.

2nd question: Normally the ad takes a while to be loaded even, this can occur to several factors whether it is a video ad or the user’s internet, the solution that I will pass you may not be the best but solves your problem, I did the part of showInterstitial() it opens the interstitial and returns true, returning it loads the code to download the firebase 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;
    }
}

0

I don’t know about Admob, but to know if the connection to Firebase Database is established there is a very easy way. It is well explained in the Google development guide in the topic How to detect the connection state of 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 believe it solves your problem. Ps: remember to use the database reference at the root of your database as in the example by entering a child won’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.