从另一个类管理adView

时间:2012-12-05 19:58:15

标签: java android android-activity admob adview

我有一个申请。有课程。例如,我有这些类:

-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,您会看到非常好的例外。

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

2 个答案:

答案 0 :(得分:0)

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

确保您在活动中实例化它 -

答案 1 :(得分: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();        
    }
}

输出

显示广告 不要显示广告