i've been searching for hours now, and i can't get a simple interstitial ad to display. the layout is within a fragment and i'm sure that's part of the problem, but i can't find a work around. any help is greatly appreciated - most of what i see is displaying the ad within an activity.
I want the interstitial to appear before the user clicks on a button in an app. i'm sure the issue is with the (this) but I can't find anything.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("example ad unit");
mapFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Constants.MAPS_INTENT_URI));
startActivity(intent);
}
});
Yes the problem is with the this. Since you are calling it in a Fragment, it won't work. Because this is the constructor from the docs:
public InterstitialAd (Context context)
The constructor requires the context. So change your code like this:
mInterstitialAd = new InterstitialAd(getContext());
To avoid NullPointerException, you can use requireContext()
mInterstitialAd = new InterstitialAd(requireContext());
Related
I am work with this guide https://firebase.google.com/docs/admob/android/interstitial
to add ad to my app and all work great when i fallow this guide
But if i want to load ad from widget,The first thing when click one widget it's don't work it's even not load the ad.
My problem is i think in this line:
mInterstitialAd = new InterstitialAd("Dont know what put here");
If you see the words "Dont know what put here" it's because the normal thing that need to be there is MainActivity.this Or this only this
But what i do when click on widget is open settings on phone
What i need to do?
This code in opUpdate inside the widget class
i Open first of all
appWidgetManager.updateAppWidget(appWidgetIds[i],views)
what i need to replace with the ad
Intent additionalCallSettingsIntent = new Intent("android.intent.action.MAIN");
ComponentName distantActivity = new ComponentName("com.android.phone", "com.android.phone.GsmUmtsAdditionalCallOptions");
additionalCallSettingsIntent.setComponent(distantActivity);
PendingIntent pInt = PendingIntent.getActivity(context, 0, additionalCallSettingsIntent ,0);
views.setOnClickPendingIntent(R.id.button,pInt);
appWidgetManager.updateAppWidget(appWidgetIds[i],views);
}
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID").build();
mInterstitialAd.loadAd(adRequest);
}
When prees on this widget:
First of all some ad like this pop up:
how i do this? thet first of all in first prees pop up ad and then go to app
I tried to implement MoPub interstitials in my libgdx android application.
When I pass a wrong ID in the MoPubInterstitial() constructor, the onInterstitialFailed() method in the listener is called correctly, but when I pass my correct ID nothing happens.
onInterstitialLoaded() should be called eventually, but it isn't.
MoPubInterstitial.isReady() never is true.
Am I missing something?
I don't know, which part of my code I should paste. I'm out of ideas, how to debug this.
Any suggestions?
private static final String MoPubInterstitialID = "xxxxxxxxxxxxx";
private MoPubInterstitial moPubInterstitial;
moPubInterstitial = new MoPubInterstitial(this, MoPubInterstitialID);
moPubInterstitial.setInterstitialAdListener(this);
moPubInterstitial.load();
moPubInterstitialButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("MoPub", "Interstitial button clicked");
if ( moPubInterstitial.isReady() )
{
Log.d("MoPub", "Interstitial is Ready");
moPubInterstitial.show();
Log.d("MoPub", "Interstial Shown");
moPubInterstitial.forceRefresh();
}
}
});
These are all the steps that i took to make it work. Let me know if you have any questions.
I'm trying to add some Admob ads into my application. So far I've made the following code into the onCreate method.
long previousAdShown = this.prefs.getLong("last_ad_shown", 0);
long now = new Date().getTime();
if (Constants.ADMOB_ENABLE && (previousAdShown < now - Constants.ADMOB_INTERVAL))
{
Log.e("ADMOB", "Load ADMOB");
// Load ad view
this.adView = new InterstitialAd(this);
this.adView.setAdUnitId(Constants.ADMOB_ID);
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice("31090DB31C8059FC6EF0FDA2XXXXXXXX");
this.adView.loadAd(adRequest.build());
// Update timer
this.prefs.edit().putLong("last_ad_shown", now).commit();
}
Which is supposed to pre-load the ads.
Then, in my Menu I got something like that:
this.menu_me_feed_layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
if (adView != null && adView.isLoaded())
{
adView.show();
adView.setAdListener(new AdListener() {
#Override
public void onAdClosed()
{
Intent intent = new Intent(LeftMenu.this.activity, UserFeedActivity.class);
intent.putExtra("isForUser", false);
LeftMenu.this.activity.startActivity(intent);
LeftMenu.this.activity.finish();
}
});
}
else
{
Intent intent = new Intent(LeftMenu.this.activity, UserFeedActivity.class);
intent.putExtra("isForUser", false);
LeftMenu.this.activity.startActivity(intent);
LeftMenu.this.activity.finish();
}
}
});
Which basically displays the ads if it was previously loaded.
I got two error:
03-26 20:44:37.437: E/GooglePlayServicesUtil(28151): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
Which, according to the Admob doc, is not a blocking error.
03-26 20:44:59.242: W/Ads(28151): Timed out waiting for ad response.
03-26 20:44:59.252: W/dalvikvm(28151): disableGcForExternalAlloc: false
03-26 20:44:59.257: W/Ads(28151): Failed to load ad: 2
I don't understand why I got those errors as my AdUnitId is correct.
Any idea ?
Thanks for your help.
The first error is benign and can be ignored.
The 2nd suggests that you didn't have a good network connection, or that the Admob server was under extreme load (unlikely). Make sure you have a good net connection and try again.
I have an Android app and I'm trying to add AdMob interstitials into it. I create them this way
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getResources().getString(R.string.ad));
interstitial.loadAd(AdUtils.createRequest());
and when the moment is appropriate, I show them like this:
if(interstitial.isLoaded()) {
interstitial.show();
}
When I test this code on the emulator, everything is fine. But when I run it on the real device, it often (about a half of all shows) displays only a black screen with the close button and no advertisement image at all. Is there any solution to this issue?
You can try as below.
Declare this as a variable.
private InterstitialAd interstitial;
then use this part in your onCreate method
interstitial = new InterstitialAd(this, "your id");
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
and at last set this outside your onCreate method so basically set below code in your activity..
#Override
public void onReceiveAd(Ad ad) {
// TODO Auto-generated method stub
if (ad == interstitial) {
interstitial.show();
}
}
Hope this will help you.
I have one Activity in my app. Now I wanted to add an Admob interstitial banner. Unfortunately the old activity is also closed when the user closes the ad.
I added something like this:
interstitial = new InterstitialAd(this, "*************");
// Create ad request
AdRequest adRequest = new AdRequest();
// and begin loading your interstitial
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
And started the ad by using
if (interstitial.isReady()){
interstitial.show();
}
The representation of the app is working fine.
What can I do to solve the problem?
There is an attribute in your AndroidManifest.xml file, like android:noHistory="true" You must delete this. it solves the problem
Do like this
private boolean isAddShown = false;
make this isAddShown = true when the add is visible
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(!isAddShown){
super.onBackPressed();
}else{
isAddShown = false;
}
}