我指的是谷歌教程,在我的应用https://developers.google.com/admob/android/interstitial中放置插页式广告。
我可以在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 :(得分:4)
我认为这可能是问题所在:
if (interstitial != null && interstitial.isLoaded()) {
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
interstitial.show();
}
});
}
您之后设置事件监听器你知道插页式广告已经加载了 - 所以我怀疑它没有第二次发射,所以你永远不会展示广告。
根据您链接的文档,将onClick
处理程序更改为:
if (interstitial.isLoaded()) {
interstitial.show();
}
(我认为您不需要检查空值,因为您在interstitial
中为onCreateView
分配了一个非空值...它永远不会为空据我所知,你点击了视图。)
答案 1 :(得分: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。
答案 2 :(得分:-1)
添加最新版本gms的依赖项:play-services-ads, 之后执行以下代码,
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();
}
}