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

标签 java android admob ads

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

好的,我会在这里用我的 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/

相关文章:

java - ReentrantLock 实际使用 lockInterruptibly

ios - 显示键盘时,Admob 广告不会出现在 UITableView 的页脚中

android - 我应该为 Admob 中的每个横幅和插页式广告设置不同的 id

android - Admob 问题 - 与 Play 商店中的应用程序链接错误

java - 匹配 IPv4 和 IPv6 地址但不匹配主机名?

java - HashMap 交换键值,反之亦然

java - Camunda BPM 执行和可变范围误解

android - 无法使用 Firebase 获取 Facebook 登录的应用密码

java - Android Studio .compareTo 和 .equals 字符串不起作用

android - 您是否有任何资源解释如何将 Firebase 测试实验室与 GitLab 管道结合使用?