如何在没有方法onCreate的情况下在其他类中添加插页式AdMob MainActivity?

时间:2017-03-13 15:45:56

标签: java android eclipse admob interstitial

我是Java的初学者。 我在Eclipse和Android Studio上打开了Android游戏。 当我想添加不包含onCreate方法的MainActivity类作为游戏的正常类时示例:暂停 - 菜单...,仍然是当前对象新的InterstitialAd(this);红色下划线。

我可以得到帮助吗? 以下是我的源代码所带来的所有数据。

//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>

1 个答案:

答案 0 :(得分:0)

传递上下文中的插页式广告方法或用于创建对象的活动。

这意味着我们可以通过上下文或活动

你可以这样写

interstitial = new InterstitialAd(activity);

其中活动是在最终代码上方声明的RacingActivity,如

  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();
        }
    });


 }