0

我有一个应用程序。有课。例如我有这些课程:

-MainActivity.java

-AppName.java

-GameScreen.java

-GamePlayScreen.java

我在 MainActivity.java 中进行了广告编码,如下所示,

public class MainActivity extends AndroidApplication
{
    protected AdView adView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout layout = new RelativeLayout(this);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,      WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = false;
    View gameView = initializeForView(new AppName(), cfg);

    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxxx");

    AdRequest request = new AdRequest();
    request.addTestDevice("xxxxxxxxxxxxxxxx");
    adView.loadAd(request);

    layout.addView(gameView);

    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

    layout.addView(adView, adParams);

    setContentView(layout);
    }
}

我有一个横幅顶部中心。这很好。这是 AppName.java:

public class AppName extends Game
{
@Override
public void create() 
{
    Assets.load();
    setScreen(new GameScreen(this, Settings.STATE_START));
}

@Override
public void resize(int width, int height)
{
    super.resize(width, height);
}

@Override
public void pause() 
{

}

@Override
public void resume() 
{

}

@Override
public void dispose()
{
    Assets.dispose();
    getScreen().dispose();
}
}

一切都很好,我仍然有一个横幅。但问题从这里开始。在我的 GameScreen 中,有一个指向 GamePlayScreen 的链接:

它去 GamePlayScreen 没有问题。

game.setScreen(new GamePlayScreen(game, i + 1));

现在我在玩游戏。但我不想在这里看到横幅。

问题是,如何在 GamePlayScreen 中禁用我的 adView?我的经验是,如果您尝试从另一个类修改 MainActivity.java,您会看到非常好的异常。

感谢您的回答和评论。我认为对于想要在他们的应用程序中集成广告的人来说,这是一个很好的例子。

4

2 回答 2

0

声明您的 adView 公共静态,您应该可以通过 activityName.AdView.setVisibility(View.GONE) 访问它;

确保你在你的活动中实例化它 -

于 2012-12-05T20:04:06.407 回答
0

例如,您可以考虑使用战略模式来改变运行时的行为

public class Game { 
    ShowAdBehavior showAdBehavior;

    void setShowAdBehavior(ShowAdBehavior qb)
    {
        showAdBehavior = qb;
    }   

    void performAd(){
        showAdBehavior.AdArea();
    }   

    public void display() {

    }
}



public class AppName extends Game {
    public AppName()
    {
        //set you default behaviour
        showAdBehavior = new CanShowAd();       
    }

}


public interface ShowAdBehavior {
    public void AdArea();
}


public class CantShowAd implements ShowAdBehavior{
    @Override
    public void AdArea() {
        // you can pass adView as a paramater here and setvisibility VISIBLE
        System.out.println("dont show ad");         
    }
}


public class CanShowAd implements ShowAdBehavior{

    @Override
    public void AdArea() {
       // you can pass adView as a paramater here and setvisibility GONE
        System.out.println("show ad");      
    }
}


public class MainClass {

    public static void main(String[] args) {

           // In your activity you can set the behavior


        AppName appName = new AppName();
        appName.performAd();        
        appName.setShowAdBehavior(new CantShowAd());        
        appName.performAd();        
    }
}

输出

显示广告 不显示广告

于 2012-12-05T21:38:17.173 回答