简体   繁体   中英

how to add Interstitial AdMob out MainActivity in other Class without the Method onCreate?

I am a beginner in Java. I had Android games that I open on Eclipse and Android Studio. When I want to add the MainActivity class that does not contain the onCreate method as the Normal Classes of the Games Example: Pause - Menu ..., still the current object new InterstitialAd (this); Is underlined by the red color.

Can I get help please ? Here is all the data to bring to my source code.

//Class PauseLayer :

import com.exemplapp.myapptetest.RacingActivity;
import com.exemplapp.myapptetest.scene.GameScene;
import com.exemplapp.myapptetest.scene.TitleScene;
import com.exemplapp.nodes.GrowButton;
import com.exemplapp.nodes.MyScene;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.transitions.CCFadeTransition;

public class PauseLayer extends MyScene {

    private InterstitialAd interstitial;

    public PauseLayer() {
        super();
        RacingActivity activity = (RacingActivity) CCDirector.sharedDirector().getActivity();
        activity.revmob();

        CCSprite sprBg = CCSprite.sprite(PATH_BG + "pause_bg-ipad.png");
        this.addChild(sprBg);
        sprBg.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

        GrowButton btnResume = GrowButton.buttonWithSpriteFileName(PATH_BTN
                + "btnResume-ipad.png", PATH_BTN + "btnResume-ipad.png",
                this, "onResume");
        this.addChild(btnResume);
        btnResume.setPosition(SCREEN_WIDTH / 2, 260 * 32 / 15);

        GrowButton btnReplay = GrowButton.buttonWithSpriteFileName(PATH_BTN
                + "btnReplay-ipad.png", PATH_BTN + "btnReplay-ipad.png",
                this, "onReplay");
        this.addChild(btnReplay);
        btnReplay.setPosition(SCREEN_WIDTH / 2, 220 * 32 / 15);

        GrowButton btnMenu = GrowButton.buttonWithSpriteFileName(PATH_BTN
                + "btnMenu1-ipad.png", PATH_BTN + "btnMenu1-ipad.png", this,
                "onMenu");
        this.addChild(btnMenu);
        btnMenu.setPosition(SCREEN_WIDTH / 2, 180 * 32 / 15);


//Admon Interstitial

        interstitial = new InterstitialAd(this);

        interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        AdRequest adRequest = new AdRequest.Builder().build();
        interstitial.loadAd(adRequest);

        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                displayInterstitial();
            }
        });


    }

    private void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }


    public void onResume(Object sender) {
        this.removeFromParentAndCleanup(true);
    }

    public void onReplay(Object sender) {
        CCDirector
                .sharedDirector()
                .replaceScene(
                        CCFadeTransition.transition(0.7f, GameScene
                                .scene(GameScene.sharedInstance().m_nGameMode)));
    }

    public void onMenu(Object sender) {
        CCDirector.sharedDirector().replaceScene(
                CCFadeTransition.transition(0.7f, TitleScene.scene()));
    }
}

//Certificate of AndroidManifest :

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.exemplapp.myapptetest.RacingActivity"
            android:label="@string/title_activity_sling_shot_racing"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.exemplapp.myapptetest.scene.game.PauseLayer"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

    </application>

Interstitial ads method in pass context or activity for object creation.

that mean we can pass then context or activity

you can write like this

interstitial = new InterstitialAd(activity);

where activity is RacingActivity that was was declare above final code look like

  public PauseLayer() {
     super();
     RacingActivity activity = (RacingActivity) CCDirector.sharedDirector().getActivity();
     activity.revmob();

    CCSprite sprBg = CCSprite.sprite(PATH_BG + "pause_bg-ipad.png");
    this.addChild(sprBg);
    sprBg.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

    GrowButton btnResume = GrowButton.buttonWithSpriteFileName(PATH_BTN
            + "btnResume-ipad.png", PATH_BTN + "btnResume-ipad.png",
            this, "onResume");
    this.addChild(btnResume);
    btnResume.setPosition(SCREEN_WIDTH / 2, 260 * 32 / 15);

    GrowButton btnReplay = GrowButton.buttonWithSpriteFileName(PATH_BTN
            + "btnReplay-ipad.png", PATH_BTN + "btnReplay-ipad.png",
            this, "onReplay");
    this.addChild(btnReplay);
    btnReplay.setPosition(SCREEN_WIDTH / 2, 220 * 32 / 15);

    GrowButton btnMenu = GrowButton.buttonWithSpriteFileName(PATH_BTN
            + "btnMenu1-ipad.png", PATH_BTN + "btnMenu1-ipad.png", this,
            "onMenu");
    this.addChild(btnMenu);
    btnMenu.setPosition(SCREEN_WIDTH / 2, 180 * 32 / 15);


   //Admon Interstitial

    interstitial = new InterstitialAd(activity);

    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    AdRequest adRequest = new AdRequest.Builder().build();
    interstitial.loadAd(adRequest);

    interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            displayInterstitial();
        }
    });


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM