Skip to content

Initialize GreedyGameAgent

Initializing GreedyGameAgent

GreedyGameAgent is the entry point to fetching Native Ads from GreedyGame SDK. You can initialize the GreedyGameAgent with either an Activity or a Context.

Initializing with an Activity instance

Create GreedyGameAgent instance in the onCreate of the Activity.

GreedyGameAgent greedyGameAgent = new GreedyGameAgent.Builder(activity)
    .setGameId(GAME_ID_CREATED) //e.g 00100100
    .addUnitId(ADUNIT_CREATED) //e.g float-1000
    .addUnitId(ADUNIT_CREATED) //e.g float-1002
    .enableAdmob(true)
    .withAgentListener(new CampaignStateListener() {
        @Override
        public void onUnavailable() {
            reloadAd(); 
            /*
            This function retries to get an ad when campaign is unvailable or onError.
            This will be defined in a later step.
            */
        }

        @Override
        public void onAvailable(String campaignId) {
            loadAd();
            /* 
            This function will be used for rendering ads.
            This will be defined in a later step .
            */
            reloadAd();
            /*
            In this context,the function will start the countdown for the refresh call, to fetch new ads.
            This will be defined in a later step.
            */
        }

        @Override
        public void onError(String error) {
            reloadAd(); 
            /*
            This function retries to get an ad when campaign is unvailable or onError.
            This will be defined in a later step.
            */
        }
    })
    .build();

Initializing with Context

If your app doesn't have an Activity, you should use this builder.

GreedyGameAgent greedyGameAgent = new GreedyGameAgent.Builder((Context) this)
    .setGameId(GAME_ID_CREATED) //e.g 00100100
    .addUnitId(ADUNIT_CREATED) //e.g float-1000
    .addUnitId(ADUNIT_CREATED) //e.g float-1002
    .enableAdmob(true)
    .withAgentListener(new CampaignStateListener() {
        @Override
        public void onUnavailable() {
            reloadAd(); 
            /*
            This function retries to get an ad when campaign is unvailable or onError.
            This will be defined in a later step.
            */
        }

        @Override
        public void onAvailable(String campaignId) {
            loadAd();
            /* 
            This function will be used for rendering ads.
            This will be defined in a later step .
            */
            reloadAd();
            /*
            In this context,the function will start the countdown for the refresh call, to fetch new ads.
            This will be defined in a later step.
            */
        }

        @Override
        public void onError(String error) {
            reloadAd(); 
            /*
            This function retries to get an ad when campaign is unvailable or onError.
            This will be defined in a later step.
            */
        }
    })
    .build();

Info

You can look at the definitions of loadAd() , reloadAd() for a better understanding of the flow.

Warning

  • Ensure that you mention the correct App ID and Unit IDs in the highlighted lines, and also for any other ad units.

To initialize GreedyGame SDK call the init() method from GreedyGameAgent instance created before.

GreedyGameAgent greedyGameAgent = new GreedyGameAgent.Builder(activity) // or Context
    .setGameId(GAME_ID_CREATED) //e.g 00100100
    .addUnitId(ADUNIT_CREATED) //e.g slot-1000
     ---"other builder methods"---
    .build();
greedyGameAgent.init();

CampaignStateListener methods

Methods Definition
onAvailable(campaignId) SDK fetched an ad
onUnavailable() Failed to fetch next ad
onError(error) SDK not able to initialize. Check the error message.