如何在第二次点击时显示插页式广告?
How to show the interstitial ads on second click?
任何人都知道当用户第二次点击按钮时如何显示插页式广告 time.I 意思是当用户点击一次按钮时广告不应该出现但是每当用户第二次点击同一个按钮时广告必须显示..
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();
}
}
How to show the interstitial ads on second click in android?
您可以获取 boolean
变量并根据该 boolean
变量管理点击事件
示例: 取一个 boolean
变量,其值为 true
比在 ClickListener
里面当用户点击 button
检查 boolean
变量是 true
意味着使用第一次点击按钮
并将 boolean
变量的值更改为 false
示例代码
take one boolean
variable
boolean isFirstTimeClick=true;
Now make your ClickListener
like this
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isFirstTimeClick){
isFirstTimeClick=false;
Toast.makeText(PicturePreviewActivity.this, "First Time Click", Toast.LENGTH_SHORT).show();
}else {
isFirstTimeClick=true;
Toast.makeText(PicturePreviewActivity.this, "Second Time Click", Toast.LENGTH_SHORT).show();
}
}
});
@Sufyan Hashmi 您需要一个 int 变量,每当该值为 2 时,每次点击其值都会增加,您应该调用 load inerestitial ad 并将变量的值分配为零。
int counter = 0;
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();
}
}
任何人都知道当用户第二次点击按钮时如何显示插页式广告 time.I 意思是当用户点击一次按钮时广告不应该出现但是每当用户第二次点击同一个按钮时广告必须显示..
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();
}
}
How to show the interstitial ads on second click in android?
您可以获取 boolean
变量并根据该 boolean
变量管理点击事件
示例: 取一个 boolean
变量,其值为 true
比在 ClickListener
里面当用户点击 button
检查 boolean
变量是 true
意味着使用第一次点击按钮
并将 boolean
变量的值更改为 false
示例代码
take one
boolean
variable
boolean isFirstTimeClick=true;
Now make your
ClickListener
like this
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isFirstTimeClick){
isFirstTimeClick=false;
Toast.makeText(PicturePreviewActivity.this, "First Time Click", Toast.LENGTH_SHORT).show();
}else {
isFirstTimeClick=true;
Toast.makeText(PicturePreviewActivity.this, "Second Time Click", Toast.LENGTH_SHORT).show();
}
}
});
@Sufyan Hashmi 您需要一个 int 变量,每当该值为 2 时,每次点击其值都会增加,您应该调用 load inerestitial ad 并将变量的值分配为零。
int counter = 0;
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();
}
}