怎么将插页式广告放在适配器类中?

本教程将介绍如何将插页式广告放在适配器类中?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

怎么将插页式广告放在适配器类中? 教程 第1张

问题描述

我在 MainActivity 中有一个 ViewPager,当您触摸适配器内的每个图像时,它会打开一个新的 Activity.

有人知道怎么在加载这些新活动之前展示广告吗?

如您所见,我有一个名为 CustomSwipeAdapterCovers 的适配器,它负责打开新活动,并在 MainActivity 中显示广告是怎么加载的,但我不知道怎么在非活动上投放广告.

public class MainActivity extends AppCompatActivity {


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


 private ViewPager viewPager;
 private CustomSwipeAdapterCapas adapter;



 //=================================

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

  // Create the next level button, which tries to show an interstitial when clicked.
  mNextLevelButton = ((Button) findViewById(R.id.next_level_button));
  mNextLevelButton.setEnabled(false);
  mNextLevelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
 showInterstitial();
}
  });



  // Create the text view to show the level number.
  mLevelTextView = (TextView) findViewById(R.id.level);
  mLevel = START_LEVEL;

  // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).
  mInterstitialAd = newInterstitialAd();
  loadInterstitial();

  // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
  Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();



  viewPager = findViewById(R.id.viewpager_main_xml);
  adapter = new CustomSwipeAdapterCapas(this);
  viewPager.setAdapter(adapter);

 }




//=====================================

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.menu_main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();

  if (id == R.id.action_settings) {
return true;
  }

  return super.onOptionsItemSelected(item);
 }


 @Override
 protected void onStop() {
  super.onStop();

 }

 @Override
 protected void onDestroy() {
  super.onDestroy();

 }

 @Override
 protected void onRestart() {
  super.onRestart();

 }

 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() {
 // Proceed to the next level.
 goToNextLevel();
}
  });
  return interstitialAd;
 }

 private void showInterstitial() {
  // Show the ad if it's ready. Otherwise toast and reload the ad.
  if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
  } else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
goToNextLevel();
  }
 }

 private void loadInterstitial() {
  // Disable the next level button and load the ad.
  mNextLevelButton.setEnabled(false);
  AdRequest adRequest = new AdRequest.Builder()
 .setRequestAgent("android_studio:ad_template").build();
  mInterstitialAd.loadAd(adRequest);
 }

 private void goToNextLevel() {
  // Show the next level and reload the ad to prepare for the level after.
  mLevelTextView.setText("Level " + (++mLevel));
  mInterstitialAd = newInterstitialAd();
  loadInterstitial();
 }

}

public class CustomSwipeAdapterCovers extends PagerAdapter {



 private int[] BookCovers = {R.drawable.cover_1,
R.drawable.cover_2,
R.drawable.cover_3,
R.drawable.capa_4,
R.drawable.capa_5,
R.drawable.capa_6,
R.drawable.capa_7,
R.drawable.capa_8,
R.drawable.capa_9,
R.drawable.capa_10,
R.drawable.capa_11,
R.drawable.capa_12,};

 private Context context;
 private LayoutInflater layoutInflater;

 public CustomSwipeAdapterCapas(Context context){

  this.context = context;

 }


 @Override
 public int getCount() {
  return capasLivros.length;
 }

 @Override
 public boolean isViewFromObject(View view, Object object) {
  return (view == (LinearLayout) object);
 }

 @Override
 public Object instantiateItem(final ViewGroup container, final int position) {

  layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View item_view = layoutInflater.inflate(R.layout.capas_layout, container, false);
  ImageButton imageButton = item_view.findViewById(R.id.imagebutton_main_xml);
  imageButton.setImageResource(BookCovers[position]);


  imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

 if( position == 0){


  Intent intent = new Intent(v.getContext(), LivroUmActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 1){


  Intent intent = new Intent(v.getContext(), LivroDoisActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 2){

  Intent intent = new Intent(v.getContext(), LivroTresActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 3){

  Intent intent = new Intent(v.getContext(), LivroQuatroActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 4){

  Intent intent = new Intent(v.getContext(), LivroCincoActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 5){

  Intent intent = new Intent(v.getContext(), LivroSeisActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 6){

  Intent intent = new Intent(v.getContext(), LivroSeteActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 7){

  Intent intent = new Intent(v.getContext(), LivroOitoActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 8){

  Intent intent = new Intent(v.getContext(), LivroNoveActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 9){

  Intent intent = new Intent(v.getContext(), LivroDezActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 10){

  Intent intent = new Intent(v.getContext(), LivroOnzeActivity.class);
  v.getContext().startActivity(intent);

 } else if(position == 11){

  Intent intent = new Intent(v.getContext(), LivroDozeActivity.class);
  v.getContext().startActivity(intent);

 }

}
  });


  container.addView(item_view);
  return item_view;
 }

 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((LinearLayout) object);
  //super.destroyItem(container, position, object);
 }

推荐答案

我也遇到过同样的问题.在您的情况下,不是从适配器活动打开插页式广告,而是从适配器活动的下一个活动打开它.以下是我的解决方案:将 Activity_B 视为您的适配器活动.

假设有3个活动,开启顺序如下:

    Suppose there are 3 activities and opening sequence is as follows:

Activity_A --> Activity_B --> Activity_C.

现在我想在 Activity_BActivity_C 之间显示插页式广告.

我先在Activity_A中加载了Interstitial Ad,然后在Activity_C中调用(或显示).

你可以像上面那样做:

在 Activity_A 即 MainActivity 中添加如下代码:

public void showme(){

  mInterstitialAd = new InterstitialAd(this);
  mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
  AdRequest adRequest = new AdRequest.Builder()
 .build();
  mInterstitialAd.loadAd(adRequest);

  mInterstitialAd.setAdListener(new AdListener()
  {

@Override
public void onAdClosed()
{
 //reload interstitial
 AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("YOUR_DEVICE_ID")
.build();
 mInterstitialAd.loadAd(adRequest);
}
  });
 }

 public static void showInterstitial() {
  if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
  }
 }

在 OnCreate 内的 Activity_A 中调用这个 showme().

在 Activity_C 中将下面的代码粘贴到 OnCreate 中:

Activity_A.showInterstitial();

此外,此方法不违反任何关于插页式广告和横幅广告的 Google Admob 政策.

好了关于怎么将插页式广告放在适配器类中?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。