【问题标题】:How can ad interstitial ads from Non Activity Class?如何从非活动类广告插页式广告?
【发布时间】:2018-04-17 02:54:03
【问题描述】:

我有一个名为 CustomBinding 的非活动类,它在主要活动上显示图像,它从资产文件夹加载图像并在点击时设置壁纸我想从非活动类显示插页式广告

package com.annasblackhat.materiallivewallpaper.util;

import android.databinding.BindingAdapter;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

/**
 * Created by Git Solution on 19/08/2017.
 */

public class CustomBinding {
    @BindingAdapter("imgAsset")
    public static void setImageAsset(ImageView imageView, String asset){
        Glide.with(imageView.getContext())
                .load(Uri.parse("file:///android_asset/"+asset))
                .into(imageView);
    }

    @BindingAdapter("imgDrawable")
    public static void setImageDrawable(ImageView imageView, String drawable){
        int img = Integer.parseInt(drawable);

        Glide.with(imageView.getContext())
                .load(img)
                .into(imageView);
    }

}

【问题讨论】:

    标签: java android admob interstitial


    【解决方案1】:

    使用您的活动的上下文加载广告,从您想要的非活动类中获取上下文类。

    private void showAd(Context context){
    
        MobileAds.initialize(context,
            "AD_ID");
    
        mInterstitialAd = new InterstitialAd(context);
        mInterstitialAd.setAdUnitId("AD_UNIT_ID");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    
        mInterstitialAd.setAdListener(new AdListener() {
           @Override
           public void onAdLoaded() {
              if (mInterstitialAd.isLoaded()) {
                  mInterstitialAd.show();
              }
           }
    
           @Override
           public void onAdFailedToLoad(int errorCode) {
               // Code to be executed when an ad request fails.
           }
    
           @Override
           public void onAdOpened() {
               // Code to be executed when the ad is displayed.
           }
    
           @Override
           public void onAdLeftApplication() {
               // Code to be executed when the user has left the app.
           }
    
           @Override
           public void onAdClosed() {
               // Code to be executed when when the interstitial ad is closed.
           }
        });
    
    
    }
    

    并通过 showAd(imageView.getContext()) 调用该方法

    但最好创建一个Singleton class 来加载和显示应用程序上下文的插页式广告。

    【讨论】:

      【解决方案2】:

      你可以像这样添加一个单例类

      public class AdClass {
      public static AdClass instance;
      InterstitialAd mInterstitialAd;
      
      public static AdClass getInstance() {
          if (instance == null) {
              instance = new AdClass();
              return instance;
          }
          return instance;
      }
      
      public void AdShow(Context context) {
          mInterstitialAd = new InterstitialAd(context);
          mInterstitialAd.setAdUnitId("ca-app-pub-40063938313***14/61589******");
          mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice("TEST_DEVICE_ID").build());
          Log.d("TAG", "GOO");
          mInterstitialAd.setAdListener(new AdListener() {
              @Override
              public void onAdLoaded() {
                  mInterstitialAd.show();
                  Log.d("TAG=", "GOO");
              }
          });
      }
      

      }

      并从另一个类或带有上下文的 Activity 调用

      AdClass.getInstance().AdShow(context);
      

      或者如果你需要Application Context那么请添加这个类

          public class MyApplication extends Application {
      
          private static MyApplication instance;
      
          @Override
          public void onCreate() {
              super.onCreate();
          }
          public static MyApplication getInstance() {
              if(instance==null)
              {
                  instance=new MyApplication();
              }
              return instance;
          }
      }
      

      别忘了在清单中添加 MyApplication 类

      <application
           .........................
          android:name=".MyApplication"
          ........................
         </application>
      

      现在用

      替换上下文
      AdClass.getInstance().AdShow(MyApplication.getInstance().getApplicationContext());
      

      【讨论】:

      • 请解决这个错误“无法解析符号上下文”
      • 如果您从 Activity 调用,则 AdClass.getInstance().AdShow(YOURACTIVITY.this); // 示例 : AdClass.getInstance().AdShow(MainActivity.this);
      • 你需要创建 MyApplication 类。请看上位解决方案
      • 清单截图photobox.co.uk/my/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多