-1

I have a non activity class named CustomBinding which shows images on main activity which loads images from the asset folder and sets wallpapers on click I want to show interstitial ad from Non Activity class

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

}
Lucifer
  • 29,392
  • 25
  • 90
  • 143

2 Answers2

0

Load the ads using context of you activity, grab the context class form you desire non-activity class.

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


}

And call that method by this showAd(imageView.getContext())

But better you make a Singleton class for loading and showing interstitial with application context.

noman404
  • 928
  • 1
  • 8
  • 23
0

You can add a singleton class like that

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

}

And call from another class or Activity with context

AdClass.getInstance().AdShow(context);

Or if you need to Application Context then please add this class

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

And don't forget to add MyApplication class in manifest

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

Now replace context with

AdClass.getInstance().AdShow(MyApplication.getInstance().getApplicationContext());