2015-11-03 105 views
-9

我指的是这个Google教程,将插页式广告放置在我的应用中,https://developers.google.com/admob/android/interstitial如何在adview中加载插页式广告

我能够在adview中成功显示横幅广告,但插页式广告未显示。我正在使用下面的代码来加载插页式广告。下面

public class DashBoardFragment extends Fragment implements OnClickListener{ 

    private ViewPager pager; 
    private ViewPagerAdapter adapter; 
    private SlidingTabLayout tabs; 
    private ArrayList<Thoughts_Model> list; 
    private Isconnected checkinternet; 
    private SessionManager session; 
    private JSONObject json_data; 
    private String userid = "", tag_json_arry = "json_array_req"; 
    private int index = 0; 
    private TextView btn_favourite,btn_share; 
    private LinearLayout progressBar_container; 
    private OnPageChangeListener pageChangeListener; 
    private LinearLayout add_container; 
    private AdView mAdView; 
    InterstitialAd interstitial; 
    AdRequest adRequest; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v =inflater.inflate(R.layout.dashboard_fragment,container,false); 
     this.session = new SessionManager(getActivity()); 

     userid = session.getuserid(); 
     checkinternet = new Isconnected(getActivity()); 

     mAdView = new AdView(getActivity()); 
     mAdView.setAdSize(Utility.getAdSize(getActivity())); 
     mAdView.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id)); 

     add_container = (LinearLayout)v.findViewById(R.id.add_container); 

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE); 
     add_container.addView(mAdView,params); 

     adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest);     // this is loading banner ad in adview properly 

     interstitial = new InterstitialAd(getActivity()); 
     interstitial.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id)); 

     interstitial.setAdListener(new AdListener() { 
      @Override 
      public void onAdClosed() { 
       requestNewInterstitial(); 
      } 
     }); 

     requestNewInterstitial(); 

     progressBar_container = (LinearLayout) v.findViewById(R.id.progressBar_container); 
     btn_favourite = (TextView) v.findViewById(R.id.btn_favourite); 
     btn_share = (TextView) v.findViewById(R.id.btn_share); 

     list = new ArrayList<Thoughts_Model>(); 
     pager = (ViewPager) v.findViewById(R.id.pager); 
     pageChangeListener = new OnPageChangeListener() { 
      @Override 
      public void onPageScrollStateChanged(int arg0) { } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { } 

      @Override 
      public void onPageSelected(int position) { 

       index = position; 
       if(list.get(position).getIsFavorite().equals("1")){ 
        btn_favourite.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_favorite_filled, 0, 0, 0); 
       }else{ 
        btn_favourite.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 
       } 
      } 
     }; 
     pager.addOnPageChangeListener(pageChangeListener); 

     tabs = (SlidingTabLayout) v.findViewById(R.id.tabs); 
     tabs.setDistributeEvenly(true); 
     tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
      @Override 
      public int getIndicatorColor(int position) { 
       return getResources().getColor(R.color.colortabstrip); 
      } 
     }); 

     btn_favourite.setOnClickListener(this); 
     btn_share.setOnClickListener(this); 
     return v; 
    } 

    private void requestNewInterstitial() { 
     // TODO Auto-generated method stub 
     interstitial.loadAd(adRequest); 
    } 

    @Override 
    public void onDestroy() { 
     mAdView.pause(); 
     super.onDestroy(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mAdView.resume(); 
     AppController.getInstance().trackScreenView("Home Screen"); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.btn_favourite: 
      if (checkinternet.isConnected()) { 
       AddFavorite(); 
      } else { 
       Utility.CommanDialog(getActivity(), 
         getResources().getString(R.string.try_again), 
         getResources().getString(R.string.no_internet), true); 
      } 
      break; 

     case R.id.btn_share: 
      //this is supposed to load ad but it toasts the message. 
      if (interstitial != null && interstitial.isLoaded()) { 
       interstitial.setAdListener(new AdListener(){ 
         public void onAdLoaded(){ 
          interstitial.show(); 
         } 
      }); 

      } else { 
       Toast.makeText(getActivity(), "Ad failed to load", Toast.LENGTH_SHORT).show(); 
      } 

      break; 

     default: 
      break; 
     } 
    } 

    private List<Fragment> getFragments(){ 
     List<Fragment> fList = new ArrayList<Fragment>(); 
     for (int i = 0; i < list.size(); i++) { 
      fList.add(ShowCategory.newInstance(list.get(i))); 
     } 
     return fList; 
    } 
} 

是XML布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:animateLayoutChanges="true" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/container_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <com.deadbrains.shareyourthought.commons.SlidingTabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:animateLayoutChanges="true" 
      android:background="@color/colorPrimary" 
      android:elevation="2dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/progressBar_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/black_trans" 
     android:gravity="center" 
     android:orientation="horizontal" > 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 
    </LinearLayout> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:animateLayoutChanges="true" > 
     </android.support.v4.view.ViewPager> 
    </FrameLayout> 

    <LinearLayout 
     android:id="@+id/add_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" 
     android:orientation="vertical" > 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/btn_favourite" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@color/colorPrimary" 
      android:drawablePadding="-10dp" 
      android:gravity="center" 
      android:paddingBottom="15dp" 
      android:paddingLeft="20dp" 
      android:paddingRight="5dp" 
      android:paddingTop="15dp" 
      android:text="Favourite" 
      android:textSize="16sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/btn_share" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="2dp" 
      android:layout_weight="1" 
      android:background="@color/colorPrimary" 
      android:drawableLeft="@drawable/ic_share_white" 
      android:drawablePadding="15dp" 
      android:paddingBottom="15dp" 
      android:paddingLeft="25dp" 
      android:paddingRight="5dp" 
      android:paddingTop="15dp" 
      android:text="Share" 
      android:textSize="16sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

</LinearLayout> 

我已经添加广告活动中表现为下面的代码,

<activity 
      android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" > 
     </activity> 

有没有错误,但广告并未显示。我哪里错了?请帮忙。

+0

downvotes?为什么?任何具体原因或者是你的广告还没有加载 –

+0

我想你还没有添加插页式编号请仔细阅读仔细阅读的链接.. – Destro

+0

把你的AndroidManifest.xml –

回答

-1

添加最新版本的大湄公河次区域的依赖关系:播放服务的广告, 是做下面的代码后,

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.InterstitialAd; 

public class Main23Activity extends AppCompatActivity { 
    // Remove the below line after defining your own ad unit ID. 
    private static final String TOAST_TEXT = "Test ads are being shown. " 
      + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID."; 

    private static final int START_LEVEL = 1; 
    private int mLevel; 
    private Button mNextLevelButton; 
    private InterstitialAd mInterstitialAd; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main23); 

     mNextLevelButton = ((Button) findViewById(R.id.next_level_button)); 
     mNextLevelButton.setEnabled(false); 
     mNextLevelButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       showInterstitial(); 
      } 
     }); 

     mLevel = START_LEVEL; 
     mInterstitialAd = newInterstitialAd(); 
     loadInterstitial(); 

    } 
    private InterstitialAd newInterstitialAd() { 
     InterstitialAd interstitialAd = new InterstitialAd(this); 
     interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); 
     interstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdLoaded() { 
       mNextLevelButton.setEnabled(true); 
      } 

      @Override 
      public void onAdFailedToLoad(int errorCode) { 
       mNextLevelButton.setEnabled(true); 
      } 

      @Override 
      public void onAdClosed() { 
      } 
     }); 
     return interstitialAd; 
    } 

    private void showInterstitial() { 
     if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
     } else { 
      Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show(); 
      goToNextLevel(); 
     } 
    } 

    private void loadInterstitial() { 
     mNextLevelButton.setEnabled(false); 
     AdRequest adRequest = new AdRequest.Builder() 
       .setRequestAgent("android_studio:ad_template").build(); 
     mInterstitialAd.loadAd(adRequest); 
    } 

    private void goToNextLevel() { 
     mInterstitialAd = newInterstitialAd(); 
     loadInterstitial(); 
    } 
} 
+0

我正在使用eclipse –

+0

好吧,然后对不起兄弟 –

0

你还没有加入的测试设备ID,在其间隙就会出现。将您的AdRequest实例更改为:

private void requestNewInterstitial() { 
     AdRequest adRequest = new AdRequest.Builder() 
        .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID") 
        .build(); 

     mInterstitialAd.loadAd(adRequest); 
    } 

将您的设备ID传递到您正在测试广告的位置。横幅和插页式广告单独使用AdRequest实例会更好。

正如所提及@SaurabhRajpal在评论中,请检查您是否使用正确的插页式广告和横幅广告ID。

+0

我添加了两个新的ID,但仍然无法正常工作。我也尝试使用测试ID仍然没有结果 –

+0

请更新您的代码,以了解您做出的任何更改 – NightFury

4

认为这可能是这个问题:

if (interstitial != null && interstitial.isLoaded()) { 
    interstitial.setAdListener(new AdListener(){ 
     public void onAdLoaded(){ 
      interstitial.show(); 
     } 
    }); 
} 

你设置事件侦听器后,你知道间质性已加载 - 所以我怀疑它不触发第二次,所以你永远不会显示广告。

按照您链接到文件,更改onClick处理程序只是:

if (interstitial.isLoaded()) { 
    interstitial.show(); 
} 

(我不认为你需要检查空,因为你要指定interstitial非空值在onCreateView ...它将永远不会为空,当你点击视图时,据我所知。)

+0

我创建了新的广告单元,更改了项目中的广告单元ID并重建并清理了项目。并开始工作。 –

+0

我对插页式广告有疑问。谢谢你的时间! http://stackoverflow.com/questions/36389729/ads-areloading-but-not-showing –

+0

对我来说,广告正在加载,但没有显示。 –