如何在按钮单击之前显示进度条,直到加载广告;在广告加载之后,隐藏进度条并再次显示没有按钮单击按钮的广告

时间:2019-06-10 15:30:52

标签: android android-progressbar rewardedvideoad

当我单击按钮时,进度栏显示,但即​​使加载,也不显示广告。仅显示Toast OnRewardedVideoAdsLoaded。我必须再次点击才能展示广告。

广告加载后,进度条停止,但未显示广告。它唯一的吐司。 当我再次点击按钮时,广告就会展示。

公共类MainActivity扩展了AppCompatActivity实现的RewardedVideoAdListener {

private ProgressBar progressBar;
private RewardedVideoAd mRewardedVideoAd;
private int coin;
private TextView bp;
private Button btn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    final Button btn = (Button) findViewById(R.id.btn);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.INVISIBLE);
    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    mRewardedVideoAd.setRewardedVideoAdListener(this);
    loadRewardedVideoAd();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View view) {
            progressBar.setVisibility(View.VISIBLE);

            showRewardedVideo();
            if (mRewardedVideoAd.isLoaded()) {

                mRewardedVideoAd.show();
            }

        }
    });


    bp = findViewById(R.id.bp);
    coin = 0;
    bp.setText(coin + "  BP");
}



private void showRewardedVideo() {

    if (mRewardedVideoAd.isLoaded()) {
        progressBar.setVisibility(View.INVISIBLE);
        mRewardedVideoAd.show();
    }
}




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

    savedInstanceState.putInt("coin", 0);

}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    int coin = savedInstanceState.getInt("coin");
}


@Override
public void onResume() {
    mRewardedVideoAd.resume(this);
    super.onResume();
}

@Override
public void onPause() {
    mRewardedVideoAd.pause(this);
    super.onPause();
}

@Override
public void onDestroy() {
    mRewardedVideoAd.destroy(this);
    super.onDestroy();
}
private void loadRewardedVideoAd() {
    if (!mRewardedVideoAd.isLoaded()) {
        mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
        mRewardedVideoAd.show();
    }
}



// "ca-app-pub-5399035702215687/5287697226"


private void addCoins(int coins) {
    coin += coins;
    bp.setText(coin + "  BP");
}

@Override
public void onRewardedVideoAdLoaded() {



    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();

}

@Override
public void onRewardedVideoAdOpened() {
    Toast.makeText(this, "onRewardedVideoAdOPENED", Toast.LENGTH_SHORT).show();

}

@Override
public void onRewardedVideoStarted() {
    Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();

}

@Override
public void onRewardedVideoAdClosed() {
    loadRewardedVideoAd();
    progressBar.setVisibility(View.INVISIBLE);
    btn.setVisibility(View.VISIBLE);
}

@Override
public void onRewarded(RewardItem reward) {
    Toast.makeText(this,
            String.format(" onRewarded! currency: %s amount: %d", reward.getType(),
                    reward.getAmount()),
            Toast.LENGTH_SHORT).show();
    addCoins(reward.getAmount());
}



@Override
public void onRewardedVideoAdLeftApplication()
{     btn.setVisibility(View.VISIBLE);
    progressBar.setVisibility(View.INVISIBLE);
}


@Override
public void onRewardedVideoAdFailedToLoad(int i) {
    Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
    progressBar.setVisibility(View.INVISIBLE);
    btn.setVisibility(View.VISIBLE);
}

@Override
public void onRewardedVideoCompleted() {
    Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
    progressBar.setVisibility(View.INVISIBLE);
    btn.setVisibility(View.VISIBLE);

}


}

1 个答案:

答案 0 :(得分:0)

我现在很好地阅读了您的问题,我明白了问题所在。准备就绪后,您不会再显示它,只是在您单击按钮时检查它的就绪状态。

最佳方法如下:

event sourcing