Question

We're using App Game Kit for one of our apps and trying to remove AdMob so we can go with Chartboost. AGK uses an interpreter for their language that passes through NDK to java. They have a class called AGKHelper which is the entry point for the commands that currently call Facebook, AdMob, an alert dialogue and some other activities. Each of the methods in AGKHelper run events through Runnable classes. Here's an example of their procedure to make an alert dialogue:

public class AGKHelper {
    //just a sample of what's in there many other methods exist
    public static void ShowMessage( Activity act, String msg )
    {
        RunnableMessage run = new RunnableMessage();
        run.act = act;
        run.msg = msg;
        act.runOnUiThread( run );
    }

}

Now the RunnableMessage class that creates the alert dialogue.

class RunnableMessage implements Runnable
{
    public Activity act;
    public String msg;

    public void run() {
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(act).create();
            alertDialog.setTitle("Message");
            alertDialog.setMessage(msg);
            alertDialog.setButton( DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which) {}});
            alertDialog.show();
            }
 }

There also exists a CreateAd() method that makes ads for AdMob using runOnUiThread(), but with a WindowManager to show the ads. As far as we can tell we MUST use Runnable to get the activity going for Chartboost. We followed Chartboost's SDK integration and we get stuck at creating our Runnable for it. All of the Runnables that AGKHelper uses also refer back to the NativeActivity by, we believe, passing it as a parameter to the method like ShowMessage() and then doing something with it in the Runnable class for each of these methods. Such as

AlertDialog.Builder(act) //for the message
ad = new AdView(act, AdSize.BANNER, pubID); //for AdMob - RunnableAd
feed.dialog(act, "feed", parameters,new DialogListener() {} // for RunnableFacebook

Unfortunately there doesn't appear to be a way to pass the NativeActivity to the Chartboost runnable. We're very inexperienced with java so we're learning as we go and feel like we make progress, but then nothing happens.

Does anyone have a full example of a Runnable class that gets Chartboost to show? We'd very much appreciate the help. I would share more of the AGKHelper class here, such as the full RunnableAd() method, but it is long and filled with WindowsManager setup. RunnableMessage is the shortest example I can provide. Thanks.

Was it helpful?

Solution

We've been able to get this running with Intent and startActivity().

AGKHelper class which is the entry point for all commands contains:

public static void CreateAd(Activity act, String publisherID, int horz, int vert, int offsetX, int offsetY)
{       
    Looper.prepare();

    Intent myIntent = new Intent(act, ChartboostActivity.class);
    act.startActivity(myIntent);
}

ChartboostActivity class:

public class ChartboostActivity extends Activity {
    private Chartboost cb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chartboostmain);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Configure Chartboost
        this.cb = Chartboost.sharedChartboost();

        String appId = "XXXXXXXXXXXXXX";
        String appSignature = "XXXXXXXXXXXXXXX";
        this.cb.onCreate(this, appId, appSignature, null);
        //this.cb.setImpressionsUseActivities(true);
        CBPreferences.getInstance().setImpressionsUseActivities(true);
    }

    @Override
    public void onStart() {
         super.onStart();
         this.cb.onStart(this);
         // Notify the beginning of a user session. Must not be dependent on user actions or any prior network requests.
         this.cb.startSession();
         // Show an interstitial
         //this.cb.showInterstitial(); 
         this.cb.showMoreApps();
         finish();
    }

    @Override
    protected void onStop() {
        //finish();
        super.onStop();

        this.cb.onStop(this);
    }

    @Override
    protected void onDestroy() {        
        //finish();
        super.onDestroy();

        this.cb.onDestroy(this);
    }

    @Override
    public void onBackPressed() {
         // If an interstitial is on screen, close it. Otherwise continue as normal.
         if (this.cb.onBackPressed())
         {
             finish();
             return;
         }
         else    
             super.onBackPressed();
    }
}

While this seems to work just fine we found it strange that we couldn't use finish() to close the loop in onDestroy() or onStop(). Ideally we'd like to do all of this in a Runnable class so that we have access to the activity and can call other methods. If anyone has an example of that it would be greatly appreciated. But this solution appears to work!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top