Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从非活动类广告中删除插播广告?_Java_Android_Admob_Interstitial - Fatal编程技术网

Java 如何从非活动类广告中删除插播广告?

Java 如何从非活动类广告中删除插播广告?,java,android,admob,interstitial,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.glid

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

}

使用活动的上下文加载广告,从非活动类中获取所需的上下文类

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


但是,最好为加载和显示应用程序上下文的间隙创建一个新的类。

您可以添加这样的单例类

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");
        }
    });
}
}

并使用上下文从另一个类或活动调用

AdClass.getInstance().AdShow(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>

如果从Activity调用AdClass.getInstance().AdShow(YOURACTIVITY.this);/,请解决此错误“无法解析符号上下文”示例:AdClass.getInstance().AdShow(MainActivity.this);您需要创建MyApplication类。请参见超级解决方案i创建我的应用程序类清单屏幕截图
AdClass.getInstance().AdShow(MyApplication.getInstance().getApplicationContext());