Last active
November 16, 2023 18:31
-
-
Save saymanrifat/71a3d5ed1952c7ab1c1e35b96ca7fb2a to your computer and use it in GitHub Desktop.
Applovin Ads, Banner, InterstitialAd & RewardedAd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=========Universel Start======================================================================================================================================= | |
// 1. Add test Device | |
// 2. Manifast Permission | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | |
<uses-permission android:name="com.google.android.gms.permission.AD_ID" /> | |
// 3 Acitivity | |
<meta-data android:name="applovin.sdk.key" | |
android:value=""/> | |
// 4 dependencies | |
implementation 'com.applovin:applovin-sdk:+' | |
----------// YourActivity | |
AppLovinSdk.getInstance(MainActivity.this).setMediationProvider("max"); | |
AppLovinSdk.initializeSdk(MainActivity.this, new AppLovinSdk.SdkInitializationListener() { | |
@Override | |
public void onSdkInitialized(final AppLovinSdkConfiguration configuration) { | |
} | |
}); | |
=========Universel End======================================================================================================================================= | |
-------------//showBanner Ads Start================================================================================================================================= | |
XML - | |
<com.applovin.mediation.ads.MaxAdView | |
xmlns:maxads="https://schemas.applovin.com/android/1.0" | |
android:id="@+id/adView" | |
android:layout_width="match_parent" | |
android:layout_height="50dp" | |
maxads:adUnitId="YOUR_AD_UNIT_ID" /> | |
Java - | |
1. | |
Activity implements MaxAdListener | |
2. | |
private MaxAdView adView; | |
3. | |
adView = findViewById(R.id.adView); | |
adView.loadAd(); | |
-------------//showBanner Ads End=============================================================================================================================== | |
-------------//showInterstitia Ads Start=============================================================================================================================== | |
1. | |
private MaxInterstitialAd interstitialAd; | |
2. | |
private void LoadInterstitialAd() { | |
interstitialAd = new MaxInterstitialAd( "YOUR_AD_UNIT_ID", this ); | |
interstitialAd.setListener( this ); | |
interstitialAd.loadAd(); | |
} | |
3. | |
LoadInterstitialAd(); | |
4. | |
if ( interstitialAd.isReady() ) | |
{ | |
interstitialAd.showAd(); | |
//code here | |
LoadInterstitialAd(); | |
} | |
5. Load Again | |
interstitialAd.loadAd(); | |
-------------//showInterstitia Ads End=============================================================================================================================== | |
-------------//showRewarded Ads Start=============================================================================================================================== | |
1. | |
implements MaxAdListener, MaxRewardedAdListener | |
2. | |
private MaxRewardedAd rewardedAd; | |
private int retryAttempt; | |
3. | |
private void LoadMaxRewardedAdAd() { | |
rewardedAd = MaxRewardedAd.getInstance("YOUR_AD_UNIT_ID", this); | |
rewardedAd.setListener(this); | |
rewardedAd.loadAd(); | |
} | |
4. | |
LoadMaxRewardedAdAd(); | |
5. | |
if (rewardedAd.isReady()) { | |
rewardedAd.showAd(); | |
// code here | |
LoadMaxRewardedAdAd(); | |
} | |
6. | |
@Override | |
public void onAdLoaded(final MaxAd maxAd) | |
{ | |
retryAttempt = 0; | |
} | |
@Override | |
public void onAdLoadFailed(final String adUnitId, final MaxError error) | |
{ | |
retryAttempt++; | |
long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) ); | |
new Handler().postDelayed(new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
rewardedAd.loadAd(); | |
} | |
}, delayMillis ); | |
} | |
@Override | |
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) | |
{ | |
rewardedAd.loadAd(); | |
interstitialAd.loadAd(); | |
} | |
@Override | |
public void onAdDisplayed(final MaxAd maxAd) {} | |
@Override | |
public void onAdClicked(final MaxAd maxAd) {} | |
@Override | |
public void onAdHidden(final MaxAd maxAd) | |
{ | |
rewardedAd.loadAd(); | |
interstitialAd.loadAd(); | |
} | |
@Override | |
public void onRewardedVideoStarted(final MaxAd maxAd) {} | |
@Override | |
public void onRewardedVideoCompleted(final MaxAd maxAd) { | |
Toast.makeText(this, "Thanks", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward) | |
{ | |
} | |
-------------//showRewarded Ads End=============================================================================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment