任何人都知道当用户第二次单击按钮时如何显示插页式广告。这意味着当用户单击一次按钮时,该广告不应显示,但是只要用户第二次单击同一按钮,则该广告必须显示..
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ShowInterstitial showInterstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showInterstitial = new ShowInterstitial(this);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.app_bar);
/*toolbar = findViewById(R.id.app_bar);
toolbar.setTitle("hell");
toolbar.*/
}
int counter = 0;
public void onClick(View view) {
if(view.getId() == R.id.ll1 ) {
counter++;
if (counter == 2) {
counter = 0;
Intent intent = new Intent(this, AggregatesActivity.class);
startActivity(intent);
if (showInterstitial != null && ShowInterstitial.isLoaded())
showInterstitial.showInterstitial();
}
}
我在不同活动中调用的和 ShowInterstitial 代码。
public class ShowInterstitial {
private InterstitialAd mInterstitialAd;
private Context context;
private boolean isAddReplace = false;
public ShowInterstitial(Context context) {
this.context = context;
mInterstitialAd = newInterstitialAd(context);
loadInterstitial();
}
private InterstitialAd newInterstitialAd(final Context context) {
InterstitialAd interstitialAd = new InterstitialAd(context);
/*if (!isAddReplace)
interstitialAd.setAdUnitId(context.getString(R.string.interstitial_one));*/
interstitialAd.setAdUnitId(context.getString(R.string.interstitial_one));
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
isAddReplace = !isAddReplace;
}
@Override
public void onAdFailedToLoad(int errorCode) {
}
@Override
public void onAdClosed() {
goToNextLevel();
}
});
return interstitialAd;
}
public boolean showInterstitial() {
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
goToNextLevel();
}
return false;
}
public void loadInterstitial() {
// Disable the next level button and load the ad.
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}
private void goToNextLevel() {
// Show the next level and reload the ad to prepare for the level after.
mInterstitialAd = newInterstitialAd(context);
loadInterstitial();
}
}
答案 0 :(得分:8)
@Sufyan Hashmi,您需要一个int变量,该变量的值将在每次单击时增加,每当该值为2时,您应调用load inestestitial ad,并将变量的值分配为零。
if
答案 1 :(得分:2)
如何在Android中第二次点击时显示插页式广告?
您可以使用boolean
变量并基于该boolean
变量来管理点击事件
示例:采用值为boolean
的{{1}}变量
当用户单击true
时,比在ClickListener
内部,检查button
变量是否为boolean
意味着第一次使用该按钮
并将true
变量的值更改为boolean
示例代码
获取一个
false
变量
boolean
现在让您的
boolean isFirstTimeClick=true;
这样
ClickListener