Since I couldn't find any documentation about this methods I couldn't find an answer.
What is the purpose of these methods?
I added this AdRequest as and my logcat went crazy. Is that wrong?
#Override
public void onDismissScreen(Ad arg0) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
These are the callback methods, using these methods you can check that what is the status of the ad.
For example if the ad is not recived, onFailedToReceiveAd() will be called.
Related
I'm showing interstitial ad in my app and ads are loading and displaying perfectly but the issue is that ads are not closing when I click cross button on back button I have to quit my app to close them
I created a new project and checked the ads in new project they are working fine and closing on back button but in my main app ads are not closing once they displayed to user
this is my Admob class
public class Admob {
public static InterstitialAd mInterstitial; // Interstital
private static AdView mAdView; // banner
public static void createLoadInterstitial(final Context context, View view)
{
mInterstitial = new InterstitialAd(context);
mInterstitial.setAdUnitId(context.getResources().getString(
R.string.admob_showIntersitial_ad_unit_id));
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
showInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
// Called when an ad leaves the app (for example, to go to the
// browser).
super.onAdLeftApplication();
}
});
loadInterstitial();
}
public static void loadInterstitial() {
mInterstitial.loadAd(new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/1033173712
build());
}
public static void showInterstitial() {
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
public static void createLoadBanner(final Context context, View view) {
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/6300978111
build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
});
}
}
This is how I show them in my activity
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
Admob.createLoadInterstitial(this,null);
I have initialize this in Mainfest also
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
I saw same questions on stackoverflow but none of them is answered the solution so any help will be really appreciated
If you are using Webiew check if there is webview is paused on ad shown
I try to integrate google Analytics to my Application but it doesn´t work.
This is my Activity:
public class InhaltsverzeichnisActivity extends Activity {
private SharedPreferencesManager prefs; //added
private InterstitialAd Interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inhaltsverzeichnis);
prefs = new SharedPreferencesManager(this); //get SharedPreferencesManager instance
int t = prefs.retrieveInt("theme", 0); //get stored theme, zero is default
ThemeUtils.setTheme(t); //Set the stored theme, will default to Black
Tracker t1 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t1.setScreenName("Inhaltsverzeichnis");
t1.send(new HitBuilders.AppViewBuilder().build());
Interstitial = new InterstitialAd(this);
Interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXX/XXXXXXXXXX");
AdRequest adRequest = new AdRequest.Builder().build();
Interstitial.loadAd(adRequest);
;
Interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
Interstitial.show();
}
});
}
protected void displayInterstitial() {
// TODO Auto-generated method stub
}
public void onDismissScreen() {
// TODO Auto-generated method stub
}
public void onFailedToReceiveAd() {
// TODO Auto-generated method stub
}
public void onLeaveApplication() {
// TODO Auto-generated method stub
}
public void onPresentScreen() {
// TODO Auto-generated method stub
}
#Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStart(this);
}
#Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStop(this);
}
If I put the OnStart and OnStop method over the Interstitial Ad , Analytics works, but the Interstitial Ad goes crazy and appears every second again,
hope anyone can help me to solve this problem thanks.
how to use ad-mob in app and what are the steps how i post ads in ad-mob how can i test in my device before uploading to Google play store.i am new to ad-mob. please explain me with sample example of it by step-wise.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.interstitialAds = new InterstitialAd(this, "ca-app-pub-****");
this.interstitialAds.setAdListener(this);
Button loadButton = (Button) this.findViewById(R.id.loadButton);
loadButton.setOnClickListener(loadButtonOnClick);
this.textView = (TextView) this.findViewById(R.id.stateTextView);
}
private OnClickListener loadButtonOnClick = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Loading Intertitial Ads");
AdRequest adr = new AdRequest();
// add your test device here
adr.addTestDevice(AdRequest.TEST_EMULATOR);
interstitialAds.loadAd(adr);
}
};
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
String message = "Load Ads Failed: (" + error + ")";
textView.setText(message);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
/**
* Called when an Activity is created in front of the app (e.g. an
* interstitial is shown, or an ad is clicked and launches a new Activity).
*/
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
if (interstitialAds.isReady()) {
interstitialAds.show();
} else {
textView.setText("Interstitial ad was not ready to be shown.");
}
}
}
Get rid of your AdListener.
Call intersitial.loadAd in your constructor
Call interstitial.show at a natural break point in your app
Call interstitial.loadAd
Go to 3.
See https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial
I need to use InterstitialAd in my app. But the problem is that after first exposure, before showing I am creating ad, call loadAd method with request, and than show (calling show method)it in the right moment. Everything seems to work good, but when I try to call show method next time it doesn't work because of isLoaded returns false.
Is there any way to load ad only once.
EDIT
Thx for answer, but the main problem is that loadAd is network connection, but the google suggest not to perform internet tasks in main thread, so what the f..k it is loading ad from the internet in main ui thread.It causes lags in my app. They are contradicting themselves.
You'd have to reload the ad anytime it's closed. I define a single method "reloadInterstitial()" and invoke it within the onCreate() method. It works great. All you have to do is call displayInterstitial() whenever you wish you to show the ad:
public class MyActivityClass extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("*****");
reloadInterstitial();
// Create ad request
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
}
public void displayInterstitial(){
if(interstitial != null && interstitial.isLoaded())
interstitial.show();
}
private void reloadInterstitial(){
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("*****");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("****");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}
private void loadInterCallBacks(){
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("xxxxxxxxxxx");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("xxxxxx");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}
I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}
adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}