如何将Interstital广告放入适配器类中? [英] How to put an Interstital Ad inside an adapter class?

查看:88
本文介绍了如何将Interstital广告放入适配器类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MainActivity 中有一个 ViewPager ,可以打开一个新的活动触摸适配器中的每个图像。



有人知道如何在加载这些新活动之前显示广告吗?



如您所见,我有一个名为 CustomSwipeAdapterCovers 的适配器,它负责打开新活动,并在 MainActivity 显示广告的加载方式,但我不知道如何在非活动中实施广告。

 公共类MainActivity扩展了AppCompatActivity {


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


私人ViewPager viewPager;
私人CustomSwipeAdapterCapas适配器;



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

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

//创建下一级按钮,单击该按钮会尝试显示插页式广告。
mNextLevelButton =((按钮)findViewById(R.id.next_level_button));
mNextLevelButton.setEnabled(false);
mNextLevelButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
showInterstitial();
}
});



//创建文本视图以显示级别编号。
mLevelTextView =(TextView)findViewById(R.id.level);
mLevel = START_LEVEL;

//创建InterstitialAd并设置adUnitId(在values / strings.xml中定义)。
mInterstitialAd = newInterstitialAd();
loadInterstitial();

//在屏幕上举起测试广告消息。在定义您自己的广告单元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){
//膨胀菜单;这会将项目添加到操作栏(如果有)。
getMenuInflater()。inflate(R.menu.menu_main,menu);
返回true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
//处理操作栏项单击此处。当您在AndroidManifest.xml中指定父活动时,操作栏将自动处理
//单击Home / Up按钮,因此
//很长。
int id = item.getItemId();

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

return super.onOptionsItemSelected(item);
}


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

}

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

}

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

}

私人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
公共无效onAdFailedToLoad(int errorCode){
mNextLevelButton.setEnabled(true);
}

@Override
公共无效onAdClosed (){
//进入下一个级别。
goToNextLevel();
}
});
return interstitialAd;
}

private void showInterstitial(){
//显示广告是否准备就绪。否则,敬酒并重新加载广告。
if(mInterstitialAd!= null&& mInterstitialAd.isLoaded()){
mInterstitialAd.show();
} else {
Toast.makeText(this,广告未加载,Toast.LENGTH_SHORT).show();
goToNextLevel();
}
}

private void loadInterstitial(){
//禁用下一级按钮并加载广告。
mNextLevelButton.setEnabled(false);
AdRequest adRequest =新的AdRequest.Builder()
.setRequestAgent( android_studio:ad_template)。build();
mInterstitialAd.loadAd(adRequest);
}

private void goToNextLevel(){
//显示下一个级别并重新加载广告以为之后的级别做准备。
mLevelTextView.setText( Level +(++ mLevel));
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}

}






 公共类CustomSwipeAdapterCovers扩展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 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
公共对象InstantiateItem(最终ViewGroup容器,最终int位置){

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) ;

}否则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.getCont ext()。startActivity(intent);

}否则if(position == 4){

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

}否则if(position == 5){

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

}否则if(position == 6){

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

}否则if(position == 7){

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

}否则if(position == 8){

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

}否则if(position == 9){

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

}否则if(position == 10){

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

}否则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(容器,位置,对象);
}


解决方案

我也非常面对同样的问题。
如果您不是从适配器活动中打开插页式广告,请从适配器活动的下一个活动中打开它。
以下是我的解决方案:
将Activity_B视为您的适配器活动。




  • 假设有3个活动

      Activity_A-> Activity_B-> Activity_C。 


  • 现在,我想显示在 Activity_B Activity_C


  • 我先将插页式广告加载到Activity_A中,然后调用(或显示)在 Activity_C 中。




您可以执行上面是这样的:



在Activity_A即MainActivity中添加代码是这样的:

  public void showme(){

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest =新的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()调用。 / p>

在Activity_C中,将以下代码粘贴到OnCreate中:

  Activity_A.showInterstitial( ); 

此外,此方法也不会违反插页式广告和横幅广告的任何Google Admob政策。


I have a ViewPager in MainActivity that open a new Activity when you touch each image that is inside an adapter.

Does anyone know how to show an ad before these new activities are loaded?

As you can see, I have an adapter called CustomSwipeAdapterCovers and it is responsible for opening the new activities, and in MainActivity show how the ad is loaded, but I don't know how to implement ads on non-activities.

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);
    }

解决方案

I have also faced this very same issue. In your case instead of opening interstitial ads from adapter activity, open it from next activity of adapter activity. Below is my Solution: Consider Activity_B as your Adapter activity.

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

    Activity_A --> Activity_B --> Activity_C.
    

  • Now I want to show interstitial Ad between Activity_B and Activity_C.

  • I have first loaded the Interstitial Ad in Activity_A and then call(or show) in Activity_C.

You can do above like this:

In Activity_A i.e. MainActivity add code like this:

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();
        }
    }

Call this showme() in Activity_A inside OnCreate.

In Activity_C paste below code inside OnCreate:

Activity_A.showInterstitial();

Also, this method doesn't violate any Google Admob policy of interstitial ads as well as banner ads.

这篇关于如何将Interstital广告放入适配器类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆