Java Question MoPub Interstitial Ads Help

Johnmcenroy

Active Member
Licensed User
Longtime User
Wanted to make interstitial ads in my MoPub library but can't understand how to implement this. Please help me with the following code (Official MoPub Code) :

B4X:
public class ExampleActivity extends Activity implements InterstitialAdListener {
    MoPubInterstitial mInterstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mInterstitial = new MoPubInterstitial(this, YOUR_INTERSTITIAL_AD_UNIT_ID_HERE);
        mInterstitial.setInterstitialAdListener(this);
        mInterstitial.load();
    }

    @Override
    protected void onDestroy() {
        mInterstitial.destroy();
        super.onDestroy();
    }

    // InterstitialAdListener methods
    @Override
    public void onInterstitialLoaded(MoPubInterstitial interstitial) {
        if (interstitial.isReady()) {
            mInterstitial.show();
        } else {
            // Other code
        }
    }


I made the following JAVA code for B4A , but it seems that it is wrong :

B4X:
package mopubint;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.AbsObjectWrapper;
import com.mopub.mobileads.MoPubInterstitial;
import com.mopub.mobileads.MoPubInterstitial.InterstitialAdListener;


@BA.Author ("Johnmcenroy")
@BA.Permissions(values={"android.permission.INTERNET","android.permission.ACCESS_NETWORK_STATE","android.permission.ACCESS_COARSE_LOCATION"})
@BA.DependsOn (values={"mopub-sdk","android-support-v4"})
@BA.ActivityObject
@BA.Version(1.0F)
@BA.ShortName("MopubInterstitial")

public class MoPubInt extends AbsObjectWrapper<MoPubInterstitial>  {
    InterstitialAdListener listener;
    MoPubInterstitial mInterstitial;
    
   public void setContentView(BA pBA, String LayoutName){
          pBA.activity.setContentView(BA.applicationContext.getResources().getIdentifier(LayoutName, "layout", BA.packageName));
      }
    
   /**
    * // Enter your Ad Unit ID from www.mopub.com
    */
   public void InitialiseInterstitial(BA pBA , String mAdUnitId)
     {
      mInterstitial = new MoPubInterstitial(pBA.activity, mAdUnitId);
      mInterstitial.setInterstitialAdListener(listener);
      mInterstitial.load();
      mInterstitial.show();
     }
}

So in B4A to make appear ad i write following code:
B4X:
Sub Globals
    Dim ads As MopubInterstitial
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ads.setContentView ("main") 'main.xml in res folder - read-only
    ads.InitialiseInterstitial("ae773452d1cf11e295fa123138070049") 'Your MoPub Unit ID
End Sub

But nothing happens.
Thanks
 

NFOBoy

Active Member
Licensed User
Longtime User
I'm tacking onto this, as I also want to be able to write wrappers for various SDK's, and I haven't found the example code to follow along perfectly yet. (as in the Game Play Services SDK)

In looking at this piece of code, I believe the issue is the listener?

In the onCreate method, the listener is set to "this", which if I understand correctly is the ExampleActivity reference, so that the hook exists to call the "onInterstialLoaded" piece of code.

I see in your code that you call the onCreate method in the InitializeInterstitial, but don't think that by declaring and using the listener is the right way to get the hook for your code? (please correct me if I'm wrong!, but if I'm wrong, please show how to do it the right way... :))

I am looking forward to Erel's port of the Game Play Services source code, as there are a few listener methods in there, so I can understand this better myself. (and I really, really want to add social integration using that library...)

Ross
 

Johnmcenroy

Active Member
Licensed User
Longtime User
Sorry , can't answer because I don't understand implementing Listner for now.
Will read more info about it. Beside also very interesting in Game Play Services Library.

I'm tacking onto this, as I also want to be able to write wrappers for various SDK's, and I haven't found the example code to follow along perfectly yet. (as in the Game Play Services SDK)

In looking at this piece of code, I believe the issue is the listener?

In the onCreate method, the listener is set to "this", which if I understand correctly is the ExampleActivity reference, so that the hook exists to call the "onInterstialLoaded" piece of code.

I see in your code that you call the onCreate method in the InitializeInterstitial, but don't think that by declaring and using the listener is the right way to get the hook for your code? (please correct me if I'm wrong!, but if I'm wrong, please show how to do it the right way... :))

I am looking forward to Erel's port of the Game Play Services source code, as there are a few listener methods in there, so I can understand this better myself. (and I really, really want to add social integration using that library...)

Ross

Yes it is full screen ad. But how it can be returned to B4A ? I used SetContentView , really don't understand something.

In most cases it is better not to use AbsObjectWrapper.

Is this intended to be a full screen ad? If yes then you can create the activity in the Java code and add it to project as a standalone activity.
 

thedesolatesoul

Expert
Licensed User
Longtime User

Johnmcenroy

Active Member
Licensed User
Longtime User
Thanks for info , but I cannot understand how to implement activity in JAVA project and return it to B4A. Sorry for so beginners questions.
I have the following code :

B4X:
public class ExampleActivity extends Activity implements InterstitialAdListener {
    MoPubInterstitial mInterstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mInterstitial = new MoPubInterstitial(this, YOUR_INTERSTITIAL_AD_UNIT_ID_HERE);
        mInterstitial.setInterstitialAdListener(this);
        mInterstitial.load();
    }

    @Override
    protected void onDestroy() {
        mInterstitial.destroy();
        super.onDestroy();
    }

    // InterstitialAdListener methods
    @Override
    public void onInterstitialLoaded(MoPubInterstitial interstitial) {
        if (interstitial.isReady()) {
            mInterstitial.show();
        } else {
            // Other code
        }
    }

How it can be returned to Standalone Activity in B4A. Totally confused with all this. Only wanted to implement these fullscreen ads.
 

thedesolatesoul

Expert
Licensed User
Longtime User

Johnmcenroy

Active Member
Licensed User
Longtime User
Thank you very much all. Finally I have done these ads. It was pretty simple how I see now. Finally my code is:

FIRST CLASS:

B4X:
package mopubint;

import com.mopub.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubInterstitial;
import com.mopub.mobileads.MoPubInterstitial.InterstitialAdListener;

import android.app.Activity;
import android.os.Bundle;

public class MoPubInt extends Activity implements InterstitialAdListener {
MoPubInterstitial mInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mInterstitial = new MoPubInterstitial(this, "ae773452d1cf11e295fa123138070049");
    mInterstitial.setInterstitialAdListener(this);
    mInterstitial.load();
}

@Override
protected void onDestroy() {
    mInterstitial.destroy();
    super.onDestroy();
}

// InterstitialAdListener methods
@Override
public void onInterstitialLoaded(MoPubInterstitial interstitial) {
    if (interstitial.isReady()) {
        mInterstitial.show();
    } else {
        // Other code
    }
}
@Override
public void onInterstitialDismissed(MoPubInterstitial arg0) {
   // TODO Auto-generated method stub
   
}

@Override
public void onInterstitialFailed(MoPubInterstitial arg0, MoPubErrorCode arg1) {
   // TODO Auto-generated method stub
   
}

@Override
public void onInterstitialShown(MoPubInterstitial arg0) {
   // TODO Auto-generated method stub
   
}
}

And SECOND CLASS:

B4X:
package mopubint;

import anywheresoftware.b4a.BA;
import android.content.Intent;

@BA.Author ("Johnmcenroy")
@BA.Permissions(values={"android.permission.INTERNET","android.permission.ACCESS_NETWORK_STATE","android.permission.ACCESS_COARSE_LOCATION"})
@BA.DependsOn (values={"mopub-sdk","android-support-v4"})
@BA.ActivityObject
@BA.Version(1.0F)
@BA.ShortName("MopubInterstitial")

public class CreateIntent {
   public Intent GetIntent(BA pBA){
        return new Intent(pBA.activity, MoPubInt.class);
    }

}

Also inside AddApplicationText () tag:
B4X:
<activity android:name="mopubint.MoPubInt" android:configChanges="keyboardHidden|orientation"/>

To SHOW ADS in B4A my code is:
B4X:
Sub Globals
    Dim ads As MopubInterstitial
End Sub

Sub Activity_Create(FirstTime As Boolean)
    StartActivity (ads.GetIntent)
End Sub
 
Last edited:

Johnmcenroy

Active Member
Licensed User
Longtime User
I wanted to share this library with community but cannot understand how to pass Unit ID from B4A to:

B4X:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mInterstitial = new MoPubInterstitial (this,"ae773452d1cf11e295fa123138070049");
    mInterstitial.setInterstitialAdListener(this);
    mInterstitial.load();
}

So how to change my Ad Unit Id "ae773452d1cf11e295fa123138070049" with user's one.

Thanks
 
Last edited:

Intelemarketing

Active Member
Licensed User
Longtime User
How Do You Post A Question

I AM OBVIOUSLY NEW TO THIS ALL, BUT I CANNOT FIND THE PLACE WHERE IT MAY SOMETHING LIKE

"NEW POST"

i NEED TO POST A QUESTION

HOW IS IT DONE PLEASE
 

Johnmcenroy

Active Member
Licensed User
Longtime User
I have done so:
in B4A code

B4X:
Sub Globals
    Dim ads As MopubInterstitial
End Sub

Sub Activity_Create(FirstTime As Boolean)
    StartActivity (ads.GetIntent("ae773452d1cf11e295fa123138070049")) 'Your MoPub Interstitial Ad Unit ID
End Sub

JAVA Code

B4X:
package mopubint;

import com.mopub.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubInterstitial;
import com.mopub.mobileads.MoPubInterstitial.InterstitialAdListener;

import android.app.Activity;
import android.os.Bundle;


public class MoPubInt extends Activity implements InterstitialAdListener {
MoPubInterstitial mInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String AdUnitID = getIntent().getExtras().getString("AdUnitID");
    mInterstitial = new MoPubInterstitial(this, AdUnitID);
    mInterstitial.setInterstitialAdListener(this);
    mInterstitial.load();
}

@Override
protected void onDestroy() {
    mInterstitial.destroy();
    super.onDestroy();
}

// InterstitialAdListener methods
@Override
public void onInterstitialLoaded(MoPubInterstitial interstitial) {
    if (interstitial.isReady()) {
        mInterstitial.show();
    } else {
        // Other code
    }
}

@Override
public void onInterstitialDismissed(MoPubInterstitial arg0) {
   // TODO Auto-generated method stub
   
}

@Override
public void onInterstitialFailed(MoPubInterstitial arg0, MoPubErrorCode arg1) {
   // TODO Auto-generated method stub
   
}

@Override
public void onInterstitialShown(MoPubInterstitial arg0) {
   // TODO Auto-generated method stub
   
}
}

AND

B4X:
package mopubint;

import anywheresoftware.b4a.BA;
import android.content.Intent;

@BA.Author ("Johnmcenroy")
@BA.Permissions(values={"android.permission.INTERNET","android.permission.ACCESS_NETWORK_STATE","android.permission.ACCESS_COARSE_LOCATION"})
@BA.DependsOn (values={"mopub-sdk","android-support-v4"})
@BA.ActivityObject
@BA.Version(1.0F)
@BA.ShortName("MopubInterstitial")


public class CreateIntent {
   
   /**
    * // Enter your Ad Unit ID from [url=http://www.mopub.com]MoPub - World's Largest Mobile Ad Server and RTB Exchange for iOS and Android[/url]
    */
   
   public Intent GetIntent(BA pBA, String AdUnitID){
        
      Intent intent1 = new Intent(pBA.activity,MoPubInt.class);
      intent1.putExtra("AdUnitID", AdUnitID);
        return intent1;
    }
}

All works fine.
 
Top