Last active
December 1, 2022 10:44
-
-
Save saymanrifat/4dc5c8f8741d3d3ea5583380ee3819e2 to your computer and use it in GitHub Desktop.
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
1. //Internet Permission : - | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | |
2. //Dependencies : - | |
//Admob Ads SDK | |
implementation 'com.google.android.gms:play-services-ads:21.3.0' | |
//firebase SDk real-time | |
implementation 'com.google.firebase:firebase-database:20.1.0' | |
3. //Meta Data : - | |
<meta-data | |
android:name="com.google.android.gms.ads.APPLICATION_ID" | |
android:value="ca-app-pub-3940256099942544~3347511713"/> | |
4. //Sting File : - | |
<string name="test_device_id">device_id</string> | |
<!-- Admob App ID--> | |
<string name="admob_APP_ID">ca-app-pub-3940256099942544~3347511713</string> | |
<!-- Admob Ad Unit IDS Here --> | |
<string name="admob_INTERSTITIAL_UNIT_ID">ca-app-pub-3940256099942544/1033173712</string> | |
<string name="admob_BANNER_UNIT_ID">ca-app-pub-3940256099942544/6300978111</string> | |
5. //Variables : - | |
private AdView mAdView; | |
private InterstitialAd mInterstitialAd; | |
6. //All Methods : - | |
//Start Methods | |
private void initAdmobAd() { | |
if (getString(R.string.test_device_id).length() > 12) { | |
//Adding your device id -- to avoid invalid activity from your device | |
List<String> testDeviceIds = Arrays.asList(getString(R.string.test_device_id)); | |
RequestConfiguration configuration = | |
new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build(); | |
MobileAds.setRequestConfiguration(configuration); | |
} | |
//Init Admob Ads | |
MobileAds.initialize(this, new OnInitializationCompleteListener() { | |
@Override | |
public void onInitializationComplete(InitializationStatus initializationStatus) { | |
} | |
}); | |
} | |
private void loadBannerAd() { | |
mAdView = findViewById(R.id.adView); | |
AdRequest adRequest = new AdRequest.Builder().build(); | |
mAdView.loadAd(adRequest); | |
} | |
private void loadFullscreenAd() { | |
//Requesting for a fullscreen Ad | |
AdRequest adRequest = new AdRequest.Builder().build(); | |
InterstitialAd.load(this, getString(R.string.admob_INTERSTITIAL_UNIT_ID), adRequest, new InterstitialAdLoadCallback() { | |
@Override | |
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) { | |
mInterstitialAd = interstitialAd; | |
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() { | |
@Override | |
public void onAdDismissedFullScreenContent() { | |
} | |
}); // FullScreen Callback Ends here | |
} | |
@Override | |
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { | |
// Handle the error | |
mInterstitialAd = null; | |
} | |
}); | |
} | |
private boolean isOnline() { | |
ConnectivityManager cm = (ConnectivityManager) getSystemService(MainActivity.CONNECTIVITY_SERVICE); | |
NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
return netInfo != null && netInfo.isConnectedOrConnecting(); | |
} | |
private void firebase() { | |
if (isOnline()) { | |
FirebaseDatabase database = FirebaseDatabase.getInstance(); | |
DatabaseReference myRef = database.getReference("status"); | |
try { | |
myRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot snapshot) { | |
//Try firebase Start==================================================== | |
try { | |
if (Boolean.TRUE.equals(snapshot.getValue(boolean.class))) { | |
initAdmobAd(); | |
loadBannerAd(); | |
loadFullscreenAd(); | |
} else { | |
Log.e("error", "Status Error"); | |
} | |
} catch (Exception e) { | |
Log.e("error", "Firebase Error"); | |
e.printStackTrace(); | |
} | |
//Try firebase End==================================================== | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError error) { | |
} | |
}); | |
} catch (Exception e) { | |
Log.e("error", "Firebase Error"); | |
e.printStackTrace(); | |
} | |
} | |
} | |
//End Methods | |
7. //Add to OnCreate : - | |
//Call All Methods. | |
firebase(); | |
8. //Show Int-Ads : - | |
if (mInterstitialAd != null) { | |
mInterstitialAd.show(MainActivity.this); | |
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() { | |
@Override | |
public void onAdDismissedFullScreenContent() { | |
loadFullscreenAd(); | |
} | |
}); | |
} else { | |
Log.d("TAG", "The interstitial ad wasn't ready yet."); | |
} | |
//The End Here | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment