简体   繁体   English

如何在不使用onCreate方法的情况下在其他类中添加非页内AdMob MainActivity?

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

I am a beginner in Java. 我是Java的初学者。 I had Android games that I open on Eclipse and Android Studio. 我有在Eclipse和Android Studio上打开过的Android游戏。 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); 当我想添加不包含onCreate方法的MainActivity类作为游戏的普通类时:暂停-菜单...,仍然是当前对象new InterstitialAd(此); 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 活动是在最终代码上方声明的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();
        }
    });


 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以调用在 MainActivity 中的同一类中声明的方法,而不是在 onCreate 或 android studio 中的其他方法中吗? - Can we call a method declared in same class in MainActivity not in onCreate or other methods in android studio? 错误:方法onCreate(Bundle)已在类MainActivity中定义 - error: method onCreate(Bundle) is already defined in class MainActivity 为什么简单的 for 循环在 onCreate 方法中有效,但在 MainActivity 类中无效? - Why does a simple for loop work in the onCreate method but not in the MainActivity class? 我如何使用onCreate方法在MainActivity中打开片段 - How can i open a fragment in MainActivity with onCreate method 从其他类调用MainActivity方法 - Calling a method of MainActivity from other class Android:不带onCreate()方法的服务类 - Android: Service Class without onCreate() method 如何在libGDX中显示Admob插页式广告 - How to show Admob interstitial in libGDX Android — MainActivity扩展类的onCreate()未被调用 - Android — onCreate() of extended class of MainActivity not called 错误:(69,20)错误:类MainActivity中已经定义了方法onCreate(Bundle) - Error:(69, 20) error: method onCreate(Bundle) is already defined in class MainActivity 如何从另一个 class 调用 MainActivity 中的方法? - How to call a method in MainActivity from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM