How I will show Interstitial ad correctly? - android

I have 3 activity A, B, and C and I am showing interstitial ad in activity B. But problem is if I instantly back in activity A, interstitial appear in activity A. If I instantly go next activity C, interstitial appear in activity C. But I want interstitial will show only in activity B. Google haven't accept my app update for this reason.

try this way
InterstitialAd mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId(getResources().getString(Your_init_id));
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.d("==","onAdLoaded");
mInterstitial.show();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Log.d("==","onAdFailedToLoad"+errorCode);
moveToOtherActivity();
}
#Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
Log.d("==","onAdClosed");
moveToOtherActivity();
}
});
mInterstitial.loadAd(adRequest);
}
private void moveToOtherActivity() {
Intent intent;
intent = new Intent(this,Activity_which_you_want_to_display_after_interstitial .class);
startActivity(intent);
finish();
}

There is a way to hide an interstitial discussed in this post
This doesn't prevent it from appearing, it just closes it.
Hope it helps

Related

Admob interstitial ad latency issues when loading ad

I am trying to implement interstitial ad in a fragment, when button is clicked the fragment doesn't open other activity , but loads data in same fragment so i can't use the they way people say interstisial ad should be used when navigating between activities. instead i am using the ad for only once when button is clicked .it works but takes sometime .how to deal with it?
it only works if user pressed the button after few seconds of launching the app.
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
Button btn=root.findViewById(R.id.btn);
initAds();
btn.setOnClickListener(this);
return root;
}
#Override
public void onClick(View view){
initAdsCallBack();
}
private void initAds(){
MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(#NonNull InitializationStatus initializationStatus) {
createNonPersonalisedAd();
}
});
}
}
private void createNonPersonalisedAd() {
Bundle networkExtrasBundle = new Bundle();
networkExtrasBundle.putInt("rdp", 1);
AdManagerAdRequest adRequest = (AdManagerAdRequest) new AdManagerAdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, networkExtrasBundle)
.build();
createInterstitialAd(adRequest);
}
private void createInterstitialAd(AdRequest adRequest){
AdManagerInterstitialAd.load(requireActivity(),getResources().getString(R.string.str_iterstitial), (AdManagerAdRequest) adRequest,
new AdManagerInterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull AdManagerInterstitialAd interstitialAd) {
// The mAdManagerInterstitialAd reference will be null until
// an ad is loaded.
mAdManagerInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
///// best place to callback is here coz its successfully loaded here
mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
createInterstitialAd(adRequest);
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mAdManagerInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mAdManagerInterstitialAd = null;
}
});
}
private void initAdsCallBack(){
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd.show(requireActivity());
debugToast("ad shown");
} else {
Log.e("Tad didn't show");
}
}
Consider pre-loading your interstitial ads to reduce latency when displaying them to your users. For more information about pre-loading your interstitial ads refer to the AdMob Interstitial Ad developer guidelines for apps developed for Android

Load interstitial ads in another activity

I'm using the basic example to add ads interstitial ads in my app with admob and firebase:
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNewGameButton = (Button) findViewById(R.id.newgame_button);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
mNewGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});
beginPlayingGame();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}
Now, I have two activities (A and B) . I want when Activity A is open, load the ads, and when Activity B is open, show the same ads loaded in A, Please help me and sorry for my bad English! I ask in this language because the English community is more effective
It seems ok for me. The ad is displayed after buttton click, if its loaded. So in case you start B activity in beginPlayingGame all will be ok. Fact that ad is started from A activity not B is detail.
Did you want somethink else?
Of course, it is possible.
You have to store interstitial instance somewhere outside the Activity class (e.g. in Application class static variable) and implement listener outside this Activity.
That's it!

Android InterstitalAd gray small bar bevor the ad

Im trying to get a InterstitialAd into my application.
It works now,
However:
1) I always get a small gray bar displayed on top of the screen during the ad is loading
2) The ad takes really long to load
any ideas how to fix these two problems?
BannerActivity:
public class BannerActivity extends Activity{
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-XXXXXX");
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded(){
mInterstitialAd.show();
}
#Override
public void onAdClosed() {
finish();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXXXXXXXXXX").build();
mInterstitialAd.loadAd(adRequest);
}
}
How i load the banner:
Intent bannerIntent = new Intent(myContext, BannerActivity.class);
myContext.startActivity(bannerIntent);
xml:
android:name="kletzgames.crazyballs.BannerActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I fixed it:
I now call the Ad during another View so it can load, and ones it loaded it will be displayed during screenTransaction.
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXX").build();
mInterstitialAd.loadAd(adRequest);
** some if's **
mInterstitialAd.show();

How to display an interstitial ad from Admob with AndEngine?

I am using andengine and I am switching between two scenes. My intention is to put an interstitial ad between the transition. This is my code so far:
public void showGameOver(){
mainactivity=new MainActivity();
mainactivity.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
interstitial = new InterstitialAd(mainactivity);
interstitial.setAdUnitId("MY AD UNIT ID");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
}
#Override
public void onAdClosed() {
// Proceed to the next level.
setCurrentScene(gameover);
res.engine.setScene(gameover);
gameover.loadResources();
gameover.create();
}
});
}
The above code( without the interstitial ad code) switches to my gameover scene. When I put the interstitial ad code, my app crashes. The interstitial ad is supposed to show the ad, and when it closes, my gameover scene should show. I put in the correct ad unit id.
Logcat says: cannot create handler inside thread that has not called looper.prepare().
I searched this up and solutions state that I must put it on the runonuithread method, which I did.
Any help will be appreciated.
You can't instantiate a new activity via mainactivity=new MainActivity(); and use that. Instead you should pass the game's underlying activity to your gameOver() method and use that:
public void showGameOver(Activity yourActivity){
yourActivity.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
interstitial = new InterstitialAd(mainactivity);
interstitial.setAdUnitId("MY AD UNIT ID");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});

Interstitial Ad not showing (with Banner ads)

I tried to add the code as per Google ADMOB Ads but it didn't work. Then I started working with GITHUB example to add Interstitial ads. I have no errors whatsoever in my project. Eclipse shows nothing in place of ads. Please have a look at my code and tell me where am I going wrong. I will highlight stuff wherever necessary. BANNER ADS ARE WORKING!!!!
public class AppGame extends Activity {
AdView adView;
/** The log tag. */
private static final String LOG_TAG = "InterstitialSample";
/** Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID1 = "INSERT AD ID HERE";
/** The interstitial ad. */
private InterstitialAd interstitialAd;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID1);
// Set the AdListener.
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.d(LOG_TAG, "onAdLoaded");
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Change the button text and disable the button.
}
});
}
/** Called when the Load Interstitial button is clicked. */
public void loadInterstitial(View unusedView) {
// Disable the show button until the new ad is loaded.
// Check the logcat output for your hashed device ID to get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Load the interstitial ad.
interstitialAd.loadAd(adRequest);
}
/** Called when the Show Interstitial button is clicked. */
public void showInterstitial(View unusedView) {
// Disable the show button until another interstitial is loaded.
if (interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Log.d(LOG_TAG, "Interstitial ad was not ready to be shown.");
}
}
/** Gets a string error reason from an error code. */
private String getErrorReason(int errorCode) {
String errorReason = "";
switch(errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorReason = "Internal error";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorReason = "Invalid request";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorReason = "Network Error";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorReason = "No fill";
break;
}
return errorReason;
}
}
Call loadInterstitial(v) after setting setting the adunit:
interstitialAd.setAdUnitId(AD_UNIT_ID1);
loadInterstitial(v);
And call showInterstitial(v) when/where you want to show the ad:
You forgot to call the loadInterstitial function for the interstitial ad to load.
In your onCreate function, call the loadInterstitial function. Then add the listener to it like so:
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.d(LOG_TAG, "onAdLoaded");
interstitialAd.show();
}
or set a boolean to true and at a natural breakpoint in your app, check if the boolean is true and then show the ad.
I have the same problem in my one of the testing device. I searched so many days. But i didn't get any solution.
At last I tried by removing the code
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
and I got successful result.
So try like this at last step of testing.
onCreate add below lines
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
}
#Override
public void onAdLoaded() {
// Ads loaded, stop requesting
mInterstitialAd.show();
//update time start
timeStart = new Date().getTime();
}
});
onResume check time show Intertitial Ads and send request Ads:
if(new Date().getTime()-timeStart>=timeShowAds) {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("36E0B62FE34536712ED1BEBD85467085")
.build();
// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest);
}

Categories

Resources