•  1
    Ajax

    How to integrate Admob and StartApp in LibGDX?

      Asmin Jismun    752        0        Report content

    Im gonna show you how to integrate Admob and StartApp in LibGDX using eclipse. I found this solution from google search and have modified some of the coding as shown below:

    To integrate admob and startapp,you must have your own publisher id for both ads provider. If you don’t have account yet, you can register to admob and for StartApp you can register here with sign up bonus!!.

    First of all you must have GooglePlayServices, BaseGameUtils and StartAppInApp included in your Android library. Im not gonna cover that part and i assume you already included it on your own.

    To call Android function from LibGDX you must create an interface class inside package that contain your class which extends game. Create a class and name it as RequestControl.

    public interface RequestControl {
       public void showAds(boolean show);
       public void showStartAppAds(boolean show);
       public void signIn();
       public void signOut();
       public void rateGame();
       public void submitScore(int score);
       public void showScores();
       public boolean isSignedIn();
    }
    

     

    After that create another class in the same package that implements RequestControl and name it as DesktopRequestControl

    public class DesktopRequestControl implements RequestControl {
    @Override
       public void showAds(boolean show) {
          // TODO Auto-generated method stub
       }
       @Override
       public void showStartAppAds(boolean show) {
       }
       @Override
       public void signIn() {
       }
       @Override
       public void signOut() {
       }
       @Override
       public void rateGame() {
       }
       @Override
       public void submitScore(int score) {
       }
       @Override
       public void showScores() {
       }
       @Override
       public boolean isSignedIn() {
          return false;
       }
    }
    


    In your class that extends game, add this code

    public class YourClassGame extends Game{
      public static RequestControl requestControl;
      public YourClassGame(RequestControl requestControl) {
         super();
         YourClassGame.requestControl = requestControl;
      }
    }
    

     

    You can call any of the interface function from all of your LibGDX package as shown below

    YourClassGame.requestControl.showStartAppAds(true); //to show StartApp ads 
    YourClassGame.requestControl.showAds(false);//to hide AdMob ads
    


    In android, inside MainActivity put this code and modified it, depends on your needs

    public class MainActivity extends AndroidApplication implements RequestControl {
       private GameHelper _gameHelper;
       private final static int REQUEST_CODE_UNUSED = 9002;
       protected AdView adView;
       /* Your ad unit id. Replace with your actual ad unit id. */
       private static final String AD_UNIT_ID = "your admob unit id";
       private final int SHOW_ADS = 1;
       private final int HIDE_ADS = 0;
       public int SHOW_START_APP = 0;
       private StartAppAd startAppAd = new StartAppAd(this);
       protected Handler handler = new Handler() {
       @Override
       public void handleMessage(Message msg) {
          switch (msg.what) {
             case SHOW_ADS: {
                adView.setVisibility(View.VISIBLE);
                break;
             }
             case HIDE_ADS: {
                adView.setVisibility(View.GONE);
                if (SHOW_START_APP == 1) {
                   startAppAd.showAd(); // show the ad
                   startAppAd.loadAd(); // load the next ad
                }
                break;
             }
          }
       }
       };
       @Override
       public void onCreate(Bundle savedInstanceState) {
          // Do the stuff that initialize() would do for you
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          super.onCreate(savedInstanceState);
          // Create the GameHelper.
          _gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
          _gameHelper.enableDebugLog(false);
          GameHelperListener gameHelperListener = new GameHelper.GameHelperListener(){
             @Override
             public void onSignInSucceeded() {
             }
             @Override
             public void onSignInFailed() {
             }
          };
          _gameHelper.setMaxAutoSignInAttempts(0);
          _gameHelper.setup(gameHelperListener);
          StartAppSDK.init(this, "Developer Id", "App Id");
          StartAppAd.init(this, "Developer Id", "App Id");
          // Create the layout
          RelativeLayout layout = new RelativeLayout(this);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
          WindowManager.LayoutParams.FLAG_FULLSCREEN);
          getWindow().clearFlags(
          WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
          // Create the libgdx View
          AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
          cfg.useAccelerometer = false;
          cfg.useCompass = false;
          cfg.useGL20 = false;
          View gameView = initializeForView(new YourClassGame(this), cfg);
          // Create and setup the AdMob view
          adView = new AdView(this);
          adView.setAdSize(AdSize.SMART_BANNER);
          adView.setAdUnitId(AD_UNIT_ID);
          AdRequest adRequest = new AdRequest.Builder().build();
          // .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
          // .addTestDevice("your actual device id").build();
          adView.setBackgroundColor(Color.TRANSPARENT);
          adView.loadAd(adRequest);
          // Add the libgdx view
          layout.addView(gameView);
          // Add the AdMob view
          RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.WRAP_CONTENT,
          RelativeLayout.LayoutParams.WRAP_CONTENT);
          adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
          adParams.addRule(RelativeLayout.CENTER_IN_PARENT);
          layout.addView(adView, adParams);
          // Hook it all up
          setContentView(layout);
       }
       @Override
       protected void onStart() {
          super.onStart();
          _gameHelper.onStart(this);
       }
       @Override
       public void onPause() {
          super.onPause();
          startAppAd.onPause();
       }
       @Override
       protected void onStop() {
          super.onStop();
          _gameHelper.onStop();
       }
       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          _gameHelper.onActivityResult(requestCode, resultCode, data);
       }
       @Override
       public void signIn() {
          try {
             runOnUiThread(new Runnable() {
             // @Override
             public void run() {
                _gameHelper.beginUserInitiatedSignIn();
             }});
          } catch (Exception e) {
          Gdx.app.log("MainActivity", "Log in failed: "   e.getMessage()".");
          }
       }
       @Override
       public void signOut() {
          try {
             runOnUiThread(new Runnable() {
             // @Override
             public void run() {
                _gameHelper.signOut();
             }});
          } catch (Exception e) {
             Gdx.app.log("MainActivity", "Log out failed: "   e.getMessage()".");
            }
       }
       @Override
       public void rateGame() {
          // Replace the end of the URL with the package of your game
          String str = "https://play.google.com/store/apps/details?id=package name";
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
       }
       @Override
       public void submitScore(int score) {
          if (isSignedIn() == true) {
             Games.Leaderboards.submitScore(_gameHelper.getApiClient(),
             getString(R.string.leaderboard_id), score);
          } else {
             // Maybe sign in here then redirect to submitting score?
          }
       }
       @Override
       public void showScores() {
          if (isSignedIn() == true)
             startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
             _gameHelper.getApiClient(),
             getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED);
          else {
             _gameHelper.beginUserInitiatedSignIn();
          }
       }
       @Override
       public boolean isSignedIn() {
          return _gameHelper.isSignedIn();
       }
       @Override
       public void showAds(boolean show) {
          handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
       }
       @Override
       public void showStartAppAds(boolean show) {
          SHOW_START_APP = (show ? 1 : 0);
          // startAppAd.showAd(); // show the ad
          // startAppAd.loadAd(); // load the next ad
       }
    }
    

     

    Inside dekstop class, modified this code

    new LwjglApplication(new YourClassGame(new DesktopRequestControl()), cfg);
    


    and for AndroidManifest.xml

    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your package namel" android:versioncode="7" android:versionname="1.6">
    <uses-sdk android:minsdkversion="8" android:targetsdkversion="19">
    <uses-permission android:name="android.permission.INTERNET">
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW">
    <uses-permission android:name="android.permission.GET_TASKS">
    <uses-feature android:glesversion="0x00020000" android:required="true">
    <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id">
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version">
    <activity android:name="your package name.MainActivity" android:configchanges="keyboard|keyboardHidden|orientation|screenSize" android:label="@string/app_name" android:screenorientation="portrait">
    <intent-filter>
    <action android:name="android.intent.action.MAIN">
    <category android:name="android.intent.category.LAUNCHER">
    </category></action></intent-filter>
    </activity>
    <activity android:name="com.google.android.gms.ads.AdActivity" android:configchanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
    </activity>
    <activity android:name="com.startapp.android.publish.list3d.List3DActivity" android:taskaffinity="your package name.AppWall" android:theme="@android:style/Theme">
    <activity android:name="com.startapp.android.publish.AppWallActivity" android:configchanges="orientation|keyboardHidden|screenSize" android:taskaffinity="your package name.AppWall" android:theme="@android:style/Theme.Translucent">
    </activity></activity></meta-data></meta-data></application>
    </uses-feature></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-sdk></manifest>
    

     

    and change your layout for android

    <?xml version="1.0" encoding="utf-8"?>
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
    <com.startapp.android.publish.banner.banner android:id="@ id/startAppBanner" android:layout_width="wrap_content" android:layout_height="wrap_content">
    </com.startapp.android.publish.banner.banner></linearlayout>
    


    I already use this code on my apps here and it s works well.

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations