AdView在首次加载时不显示

时间:2018-03-23 23:45:18

标签: java android google-advertising-id

我一直致力于学习如何制作游戏,而我喜欢了解如何添加广告。广告显示?但只有在我关闭并重新打开后,“SMART_BANNER”才能正常工作。我做错了什么?

public class MainActivity extends FragmentActivity {

    public GoogleApiClient apiClient;
    private MainActivity main = this;
    public GameSurface gameSurface;
    RelativeLayout layout;
    RelativeLayout adlayout;
    private Saver saver;

    private static final String HIGHSCORE = "highscore";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apiClient = new GoogleApiClient.Builder(this)
                .addApi(Games.API)
                .addScope(Games.SCOPE_GAMES)
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(getBaseContext(), "Connection To Google Games Failed, No App Found Or No Internet", Toast.LENGTH_SHORT).show();
                    }
                }).build();

        MobileAds.initialize(this, getString(R.string.adappid));

        apiClient.connect();
        saver = Saver.getInstance(this);

        playerscores();

        // fullscreen
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        layout = new RelativeLayout(this);
        layout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        adView.loadAd(adRequestBuilder.build());

        //ads
        gameSurface = new GameSurface(this, main);
        layout.addView(gameSurface);

        layout.addView(adView);

        setContentView(layout);

    }
            // Set No Title
            //this.setContentView(new GameSurface(this));

    public void playerscores() {
        if (apiClient != null && apiClient.isConnected()) {
            Games.Leaderboards.loadCurrentPlayerLeaderboardScore(apiClient, "CgkI08DA0-sZEAIQAQ", LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
                    new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {

                        @Override
                        public void onResult(Leaderboards.LoadPlayerScoreResult arg0) {
                            LeaderboardScore c = arg0.getScore();
                            String score = c.getDisplayScore();
                            saver.saveString(HIGHSCORE, score);
                        }
                    });
        }
    }

    public void gameover() {
        if (apiClient != null && apiClient.isConnected()) {
            Games.Leaderboards.submitScore(apiClient, getString(R.string.leaderboard_highscores), GameSurface.HighScore);
        }
    }

    public void showLeaderboard() {
        if (apiClient != null && apiClient.isConnected()) {
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(apiClient, "CgkI08DA0-sZEAIQAQ"), 1);
        } else {
            apiClient.connect();
        }
    }
}

基本上我希望广告在应用打开后立即显示。我同样喜欢它固定在底部,我无法找到一种方法来做到这一点,我尝试添加重力,但adview没有这个属性。

对于我做错的任何建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

部分答案我研究了如何让广告与底部对齐并居中。

RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);viewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        adView.setLayoutParams(viewParams);

编辑[已解决]

添加了一个adview听众,通过admob文档阅读并看到各种听众给了我一个想法! 然后让它重新绘制自己。

 adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adView.setVisibility(View.GONE);
                adView.setVisibility(View.VISIBLE);
            }
        });

答案 1 :(得分:0)

您应该在加载广告之前添加视图,如果您希望更快地展示广告,请尽快调用setContentView(),然后加载广告