来自非活动类的广告插页式广告如何?

时间:2018-04-17 02:54:04

标签: java android admob interstitial

我有一个名为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);
    }

}

2 个答案:

答案 0 :(得分:0)

使用您的活动上下文加载广告,抓取您希望非活动类的上下文类表单。

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加载并显示应用程序上下文的插页式广告。

答案 1 :(得分:0)

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

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());