android在服务中显示Admob或Appodeal广告

时间:2018-07-07 00:29:29

标签: java android admob appodeal

我想在服务中显示admob或adodeal广告,并且该应用不会在最近的应用中显示。 我尝试了很多方法,但是没有用。

我是这样找到的,但是该应用会出现在最近的应用中!

‌‌‌‌‌‌‌‌‌‌‌‌ ‌‌‌

public class ServiceAd extends Service {

  private InterstitialAd mInterstitialAd;


  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();

    mInterstitialAd = new InterstitialAd(getApplicationContext());
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    mInterstitialAd.setAdListener(new AdListener() {
      @Override
      public void onAdClosed() {
        // Load the next interstitial.
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        Log.d("tag", "on closed ad");
      }

    });

  }

  @Override
  public int onStartCommand(final Intent intent, int flags, int startId) {

    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        if (mInterstitialAd.isLoaded()) {
          mInterstitialAd.show();
        } else {
          Log.d("tag", "The interstitial wasn't loaded yet.");
        }
      }
    }, 5000);   // 5 mins
    return START_STICKY;
  }
}

0 个答案:

没有答案