gpt4 book ai didi

java - 设置点击按钮时显示广告

转载 作者:行者123 更新时间:2023-12-01 11:12:23 26 4
gpt4 key购买 nike

我这里有一个小问题。如何让广告在点击按钮时显示?

好的,我会在这里用我的 menu.class 进行解释

  public class menu extends Activity {

private InterstitialAd mInterstitialAd;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);

// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(menu.this);

// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId("ca-app-pub-3502149848009990/xxxxxxxxx");

// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();

// Begin loading your interstitial.
mInterstitialAd.loadAd(adRequest);

// Prepare an Interstitial Ad Listener
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});

Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent i =new Intent(getApplicationContext(), aselectmenu.class);
startActivity(i);
}
});

Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(menu.this);
openDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
openDialog.setContentView(R.layout.inflatequitapp);

TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);

Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}

public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
@Override
public void onBackPressed() {
// do nothing.
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

我的问题在这一行

 Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(amenu.this);
openDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
openDialog.setContentView(R.layout.inflatequitapp);

TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);

Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}

如何将 displayInterstitial() 方法放在那里,以便在单击按钮时显示广告?

最佳答案

在finish()之前调用displayInterstitial()方法:

dialogExitButton.setOnClickListener(new OnClickListener(){                                      
@Override
public void onClick(View v) {
displayInterstitial();
finish();
}
});

但是,您应该注意,根据 disallowed admob implementations :

App load or exit

Do not place interstitial ads on app load and when exiting apps as interstitials should only be placed in between pages of app content.

不遵守此政策可能会导致您的应用无法转换广告。

因此,为了安全起见,在从一个屏幕转换到另一个屏幕期间显示广告,最好在播放按钮的事件处理程序中显示:

  Button play = (Button)findViewById(R.id.play);           
play.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent i =new Intent(getApplicationContext(), aselectmenu.class);
displayInterstitial();
startActivity(i);
}
});

关于java - 设置点击按钮时显示广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com