2015-12-23 19 views
7

Tôi muốn hiển thị biểu ngữ toàn màn hình trong ứng dụng Android của mình.AdMob Quảng cáo xen kẽ (toàn màn hình) sẽ không hiển thị

Trong onCreate tôi gọi chức năng này:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    showInterstitial(); 
} 

chức năng của tôi:

private void showInterstitial() { 

    interstitialAd = new InterstitialAd(this); 
    interstitialAd.setAdUnitId(getString(R.string.ad_banner_id)); 
    interstitialAd.show(); 

    Toast.makeText(this, "Ad will load", Toast.LENGTH_SHORT).show(); 
} 

My App sẽ sụp đổ với tin nhắn này:

Nguyên nhân: java.lang.IllegalStateException : ID đơn vị quảng cáo phải được đặt trên Quảng cáo xen kẽ trước khi chương trình được gọi.

Nhưng tôi đặt id quảng cáo trước khi hiển thị hay không?

+0

bạn đã thêm banner_ad_unit_id vào tài nguyên như tệp string.xml chưa? –

+0

có id này sẽ được hiển thị trong đăng nhập mèo, nếu tôi đưa nó ra với Log.d tôi đặt id để kiểm tra trực tiếp: interstitialAd.setAdUnitId ("xxxxx"); cùng một lỗi – SpecialFighter

+0

@DhawalSodhaParmar nó không quan trọng. Ngay cả id đơn vị quảng cáo biểu ngữ sẽ tìm nạp quảng cáo trung gian. Mặc dù nó hoạt động, nó không được khuyến khích để làm như vậy. – AndroidMechanic

Trả lời

9

Bạn không gọi loadAd() cho quảng cáo trung gian. Quảng cáo trung gian sẽ tải trước khi bạn có thể hiển thị quảng cáo.

interstitialAd.loadAd(adRequest); 

bạn cũng nên kiểm tra xem nó có được tải trước khi gọi show() hay không. Nó có thể không có sẵn ngay lập tức và bạn có thể muốn giữ nó trước khi bạn gọi.

if(mInterstitial.isLoaded()){ 
      mInterstitial.show(); 
      AdRequest adRequest = new AdRequest.Builder().build(); 
      mInterstitial.loadAd(adRequest); //optionally load again if you plan to show another one 
     } 

có thể thực hiện (thay đổi nó để phù hợp với yêu cầu của bạn)

Vì vậy, về cơ bản sau đây có thể đi trong onCreate()

interstitialAd = new InterstitialAd(this); 
    interstitialAd.setAdUnitId(getString(R.string.ad_banner_id)); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    interstitialAd.loadAd(adRequest); 
    Toast.makeText(this, "Ad will load", Toast.LENGTH_SHORT).show(); 

showInterstitial() trở này

private void showInterstitial() { 
     if(mInterstitial.isLoaded()){ 
       mInterstitial.show(); 
       //optionally load again if you plan to show another one later 
       AdRequest adRequest = new AdRequest.Builder().build(); 
       mInterstitial.loadAd(adRequest); 
      } 
    } 

LƯU Ý: gọi showInterstitial() khi bạn muốn hiển thị quảng cáo xen kẽ. Nhưng, không ngay sau khi gọi loadAd(). Sẽ mất vài phút để tải quảng cáo liên kết và bạn có thể bỏ lỡ một phần nhỏ giây nếu mạng bị chậm hoặc nội dung quảng cáo nặng hơn bình thường.

Ngoài ra, đây là tài liệu để triển khai Admob Intersitials đúng cách.

+0

nếu tôi thử mã này, load() và isReady() sẽ có màu đỏ trong studio android (Không thể giải quyết các phương thức) – SpecialFighter

+0

xin lỗi đó là loadAd và isLoaded ... tôi sẽ sửa đổi câu trả lời. Ở trên là cho mopub nhưng hoạt động trong một thời trang tương tự. – AndroidMechanic

+0

kiểm tra câu trả lời cập nhật – AndroidMechanic

1

thêm bên dưới thông số đầu vào com.google.android.gms.ads.AdView

ads:adSize="BANNER" 
ads:adUnitId="@string/banner_ad_unit_id" 

<com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/banner_ad_unit_id"> 
    </com.google.android.gms.ads.AdView> 

xác minh rằng bạn đã sử dụng bên dưới dòng trong cách bố trí cha mẹ của bạn

xmlns:ads="http://schemas.android.com/apk/res-auto" 

just verify code

2
public class LoadAd { 

    private final AdView ad; 

    InterstitialAd interstitialAd; 
    boolean isInterstitialShown = false; 

    public LoadAd(Context con) 
    { 
     if (con.getClass().getName() == MainActivity.class.getName()) 
     { 
      interstitialAd = new InterstitialAd(con); 
      interstitialAd.setAdUnitId("ca-app-pub-8037008543529602/2054128571"); 

      AdRequest adRequest = new AdRequest.Builder().build(); 
      interstitialAd.loadAd(adRequest); 

      interstitialAd.setAdListener(new AdListener() 
      { 
       @Override 
       public void onAdLoaded() 
       { 
        super.onAdLoaded(); 
       } 

       @Override 
       public void onAdOpened() 
       { 
        super.onAdOpened(); 
        isInterstitialShown = true; 
       } 


       @Override 
       public void onAdFailedToLoad(int errorCode) 
       { 
        super.onAdFailedToLoad(errorCode); 
       } 
      }); 

     } 

     ad = (AdView) ((Activity)con).findViewById(R.id.adView); 
    } 


    public void init() 
    { 
     if (ad != null) 
     { 
      AdRequest req = new AdRequest.Builder().build(); 
      ad.loadAd(req); 
     } 
    } 


    public void destroy() 
    { 
     if (ad != null) 
     { 
      ad.destroy(); 
     } 
    } 


    public boolean showInterstetial() 
    { 
     if (isInterstitialShown) 
     { 
      return false; 
     } 
     if (interstitialAd != null) 
     { 
      if (interstitialAd.isLoaded()) 
      { 
       interstitialAd.show(); 
      } 
      else 
      { 
       return false; 
      } 
      return true; 
     } 
     return false; 
    } 
} 
Các vấn đề liên quan